2017-08-17 85 views
-1

我有一個SQL表格照顧我網站的成員列表。但是,註冊新用戶時,數據不會插入表中。沒有錯誤顯示,因爲我在我的代碼中檢查了幾次。SQL - 未插入數據,無錯誤

if (empty($error_msg)) { 

     // Create hashed password using the password_hash function. 
     // This function salts it with a random salt and can be verified with 
     // the password_verify function. 
     $password = password_hash($password, PASSWORD_BCRYPT); 

     // Insert the new user into the database 
     if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password) VALUES (?, ?, ?)")) { 
      $insert_stmt->bind_param('sss', $username, $email, $password); 

      // Execute the prepared query. 
      if (!$insert_stmt->execute()) { 
       header('Location: ./error.php?err=Registration failure: INSERT'); 
      } 

      header('Location: ./register_success.php?'); 
     } 
    } 

該代碼執行到最後,我被重定向到register_success。 我知道這不是因爲我在代碼中的某處輸入了錯誤的數據庫憑據,而是使用相同的憑據將數據發送到同一數據庫(它們保存在另一個文件中),並且它工作得很好。

編輯: 尼克·科恩斯說,刪除第二個「標題」後,我被重定向到錯誤頁面,這意味着執行有問題。不過,我不知道爲什麼......

+2

檢查的代碼在哪裏顯示任何錯誤?此外,無論是否有錯誤,它都將重定向到register_success.php,因爲您的第二個「header()」函數將會執行,而且將覆蓋第一個。 –

+0

@NickCoons哦,我不知道第二個會覆蓋第一個。然後,在那種情況下,我只刪除了第二個,然後我被重定向到錯誤頁面,這意味着執行有問題。不過,我不知道爲什麼。 – Zerox029

+0

看看這個輸出發生的錯誤:http://php.net/manual/en/mysqli.error.php –

回答

1

你會想要使用the documentation on this page來確定什麼是SQL錯誤,如果有的話。

此外,如果出現錯誤,您將執行兩次header()函數,而它的第二個實例將覆蓋第一個實例(因爲您只能發送一個「位置」標題),所以它將重定向到您的成功的網頁無論如何。我建議在您的if聲明的else條款中添加第二個header()呼叫,或者在第一次呼叫header()後添加exit(),以便只執行一個或另一個。