准备语句MySQLI:INSERT不起作用

时间:2016-08-12 07:20:41

标签: php sql mysqli prepared-statement

我是编码初学者,无法解决此错误。我尝试创建登录/注册脚本,但我的INSERT语句不起作用,我无法找到错误:/ Sry因为我的英语不好,我是德国人。

"致命错误:在"

中调用boolean上的成员函数bind_param()
function getIndexOfDiv() {
   var divList = Array.prototype.slice.call(document.getElementsByClassName('new-box'));

   // pass the currently clicked div
   return divList.indexOf(this);
}

1 个答案:

答案 0 :(得分:0)

该错误表明prepare语句已失败,但不清楚哪一个。以下代码未经过测试,我想知道u上的重音是否可能导致问题(?),因此我将该变量重命名为$hash

<?php

    if( !empty( $_POST['registrieren'] ) && !empty( $_POST['name'] ) && !empty( $_POST['password'] ) && !empty( $_POST['g-recaptcha-response'] ) ){

        $url        = 'https://www.google.com/recaptcha/api/siteverify';
        $privateKey = "6LdBNScTAAAAALrn5__S9lfV3EuSFu9Si_gwWeus";
        $response   = file_get_contents( $url . "?secret=" . $privateKey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR'] );
        $data       = json_decode( $response );


        if( isset( $data->success ) && $data->success == true ) {
            $name           = $_POST['name'];
            $password       = $_POST['password'];
            $stmt           = $db->prepare("SELECT `name` from `users` WHERE `name` = ?;");

            if( !$stmt ){
                exit('Error preparing sql select statement');
            }

            $stmt->bind_param( 's', $name );
            $stmt->execute();


            if ( $stmt->num_rows ) {

                echo "<div class='fehler'>Name bereits vergeben!</div>";

            } else {

                /* release results from previous cmd */
                $stmt->free_result();

                /* Is it possible that the accent on the 'u' caused problems? */
                $hash = password_hash( $password, PASSWORD_DEFAULT );

                $stmt = $db->prepare( "INSERT INTO `users` (`name`, `password`) VALUES (?, ?);" );
                if( !$stmt ){
                    exit('Error preparing sql insert statement');
                }
                $stmt->bind_param( "ss", $name, $hash );  
                $stmt->execute();

                /* again, free the results */
                $stmt->free_result();

                /* do you really want to store a password in a session variable? */
                $_SESSION['name'] = $name;
                $_SESSION['password'] = $password;


                header("Location: http://localhost/data/login/me.php");
            }
        } else {
            echo "<div class='fehler'>Captcha-Check failed!</div>";
        }
    }
?>