Search code examples
phpsqlmysqliprepared-statement

Prepared-statement MySQLI: INSERT doesn't work


I am a coding beginner and can't solve this error.I tried to create a login/register script but my INSERT statement doesn't work and I can't find the error:/ Sry for my bad english, I am german.

"Fatal error: Call to a member function bind_param() on boolean in"

  if (isset($_POST['registrieren']) && $_POST['name'] != "" && $_POST['password'] != "")
  {
    $url        = 'https://www.google.com/recaptcha/api/siteverify';
    $privateKey = "???????????????????????????";

    $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'];
        $username_exists = $db->prepare("SELECT name from users WHERE name = ? ");
        $username_exists->bind_param('s', $name);
        $username_exists->execute();

        if ($username_exists->num_rows) {
            echo "<div class='fehler'>Name bereits vergeben!</div>";
        } else {
            $verschlüsseln = password_hash($password, PASSWORD_DEFAULT);
            $insert = $db->prepare("INSERT INTO users (name, password) VALUES (?, ?)");
            $insert->bind_param("ss", $name, $verschlüsseln);  
            $insert->execute();
            $_SESSION['name'] = $name;
            $_SESSION['password'] = $password;
            header("Location: http://localhost/data/login/me.php");
        }
    } else {
      echo "<div class='fehler'>Captcha-Check failed!</div>";
    }
}

Solution

  • The error suggests that the prepare statement has failed but it's not clear which one. The code below is not tested and I wonder whether the accent on the u might have caused issues (?) so I renamed that variable to $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>";
            }
        }
    ?>