0

I am just starting out with PHP and SQL and I am trying to make a account creation screen for a website in PHP. For this I've created a form file with an action to the second file where the input data from the form is supposed to be inserted into TWO TABLES of a local database. (I am using WAMP with phpMyAdmin) -

My (database) connection is up and everything and I have no errors, and when go to phpMyAdmin and do the SQL statement with dummy data it works, but when the user fills out the form and clicks on next it doesn't get inserted into the database? Anyone have a idea how to fix this?

Here is my current php code where the data is supposed to be inserted into 2 different tables via the SQL statement:

<?php
    $_SESSION['user'] = $_POST['name']; //user naam ophalen van vorige pagina en onthouden voor gehele sessie
$name= $_SESSION['user'];

    $_SESSION['wachtwoord'] = $_POST['access']; 
$access= $_SESSION['wachtwoord'];

    $_SESSION['mail'] = $_POST['email'];
$email= $_SESSION['mail'];

    $_SESSION['huisdier'] = $_POST['pet'];
$pet= $_SESSION['huisdier'];

$savenewuser = "BEGIN; 
                INSERT INTO user (id, naam, email, pass)
                VALUES (NULL, '$name', '$email', '$access');
                INSERT INTO preferences (id, huisdier)
                VALUES (LAST_INSERT_ID(), '$pet');
                COMMIT;
                ROLLBACK;";

mysqli_query($con, $savenewuser);

$result = mysqli_query($con, $savenewuser);
if (!$result) {
echo "Error: Account could not be created, try again later.";
} else { 
echo "Account has been created!";
}

mysqli_close($con);
?> 

When executed I do get the message "Error: Account could not be created, try again later." of course. - I think I have to change something about the SQL statement but how exactly I am unsure of.

7
  • You need to put session_start() at the top of the code. You have nothing assigned to $name, $email, $access Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Use real MySQLi error messages. Commented Mar 10, 2015 at 12:22
  • @JayBlanchard I've done that, this is what I have at the very top of my file: <?php session_start(); //start sessie require_once("connectiecheck.php"); ?> Commented Mar 10, 2015 at 12:23
  • try to change $result = mysqli_query($con, $savenewuser); instead of $result = mysqli_query($con, $savenewuser) or die (mysqli_error($con)) ; Commented Mar 10, 2015 at 12:34
  • @JayBlanchard I just added in exactly what you suggested and added in what Lord_Linus suggested. My current error that I am getting is: mysql_error() expects parameter 1 to be resource Commented Mar 10, 2015 at 15:01
  • @lord_linus added in your suggestion. Current single error i am getting is: mysql_error() expects parameter 1 to be resource Commented Mar 10, 2015 at 15:01

1 Answer 1

1

mysqli_query is being called twice. If you call it twice the second time will error because you are trying to give 2 people the same id (Assuming you set your database up correctly).

If that doesn't work, I would try replacing mysqli_query with mysqli_multi_query.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! your answer solved my issue, I had to change it to mysqli_multi_query. Appreciate it a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.