2016-08-12 65 views
0

我是一個編碼初學者,無法解決這個錯誤。我嘗試創建一個登錄/註冊腳本,但是我的INSERT語句不起作用,我無法找到錯誤:/對我的英語不好,我是德語。準備語句MySQLI:INSERT不起作用

「致命錯誤:調用布爾成員函數bind_param()在」

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>"; 
    } 
} 
+0

在'bind_param' 3個參數,但在'INSERT'只有2 –

+0

你有沒有檢查數據庫中是否插入數據。 –

+0

你在哪裏可以看到3個參數? $ insert-> bind_param(「ss」,$ name,$verschlüsseln); $ insert-> bind_param(「ss」,$ name,$verschlüsseln); – P2scal

回答

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>"; 
     } 
    } 
?> 
+0

我認爲這將工作@ P2sca –

+0

謝謝! free_result();解決了這個問題。 – P2scal