douzhan1238 2014-03-20 11:55
浏览 32
已采纳

too long

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...

  • 写回答

1条回答 默认 最新

  • dongzhuo1733 2014-03-20 12:04
    关注

    Free() is method of the result object.

    When used with statement object use free_result()

    When adjusted to your code sample it should look smth like:

        $some_result = $result->execute();
        // ... code ...
        $some_result->free();
    

    or

        $result->execute();
        // ... code ...
        $result->free_result();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab