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.
session_start()at the top of the code. You have nothing assigned to$name, $email, $accessAdd error reporting to the top of your file(s) right after your opening<?phptagerror_reporting(E_ALL); ini_set('display_errors', 1);Use real MySQLi error messages.