I'm using php and mysqli to access a database and make queries, but I'm getting an error I'm not able to remove:
Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Commands out of sync; you can't run this command now' in /home/a4689621/public_html/staging/includes/functions.php:214 Stack trace: #0 /home/a4689621/public_html/staging/includes/functions.php(214): mysqli->prepare('INSERT INTO mem...') #1 /home/a4689621/public_html/staging/register.php(62): register('litiales@gmail....', 'Alessandro', 'Liti', 'Umbria', 'Maschio', '1991-05-12', 'Alessandro91') #2 {main} thrown in /home/a4689621/public_html/staging/includes/functions.php on line 214
here is my code, I hope you can help me:
function register($email, $nome, $cognome, $regione, $sesso, $date, $password) {
require ("db_connect.php");
if (!$mysqli->connect_error) {
$result = $mysqli->prepare("SELECT id FROM members WHERE email=? LIMIT 1");
$result->bind_param('s', $email);
$result->execute();
if ($result->num_rows == 1) {
// A user with this email address already exists
header('Location: ../error.php?err=Registration failure: email already present');
}
$result->free();
} else {
echo mysqli_connect_error();
}
echo 'ciao';
//effettuo l'inserimento
if (empty($error_msg)) {
// Create a random salt
$random_salt = hash('sha512', md5(uniqid(rand(), TRUE)));
//Create a confirmation code
$com_code = md5(uniqid(rand()));
// Create salted password
$password = hash('sha512', $password . $random_salt);
if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt, conf_code, name, surname, dob, sex, location) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssssssssss', $username, $email, $password, $random_salt, $com_code, $nome, $cognome, $data, $sesso, $regione);
// Execute the prepared query
if ($insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
} else {
$too = "admin@alessiatesi.com";
$subject = "Conferma la tua iscrizione al sito";
$message = "Collegati all'indirizzo qui sotto per attivare il tuo account:";
$message .= "http://staging.alessiatesi.net63.net/confirm.php?passkey=$com_code&user=$username";
$headers = "From: $too
";
$headers .= "Reply-To: $too
";
$headers .= "Return-Path: $too
";
$headers .= "X-Mailer: PHP/" . phpversion() . "
";
$headers .= 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; UTF-8' . "
";
if (mail($email, $subject, $message, $headers))
header('Location: ../register_success.php');
else {
$restore_stmt = $mysqli->prepare("DELETE FROM members WHERE username=?");
$restore_stmt->bind_param('s', $username);
$restore_stmt->execute();
header('Location: ../error.php?err=impossibile inviare la mail di conferma. Riprovare.');
}
}
}
} else {
header('Location: ../error.php?err=Mail not valid');
}
}
The line 214 is:
$insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt, conf_code, name, surname, dob, sex, location) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
I've tried to do everything (from upgrading mysql to upgrade php), changed from prepared statement to query, but nothing...