2016-12-08 35 views
1

我試圖通過綁定參數來防止sql注入,但是當我將代碼轉換爲綁定參數時,mysqli_num_rows不再起作用。mysqli_num_rows提供參數後無法工作

我有我要檢查對數據庫的重複行的簡單的電子郵件驗證:

下面我的代碼:

$checkDup = "SELECT Email FROM users WHERE Email='{$_POST['Email']}'"; 
    $resultDup = mysqli_query($db,$checkDup); 

    //If not 0 duplicates (another one exists) create an error alert 
    if(!mysqli_num_rows($resultDup) == 0){ 
     echo '<script language="javascript"> 
       alert("Email Already Exists"); 
       window.location.href = "Sign Up.php"; 
      </script>'; 
     unset($_POST); 
    } 

結合它之後 -

Its Not right type error i got :

$checkDup = $db->prepare("SELECT Email FROM users WHERE Email= ?"); 
$checkDup->bind_param("s", $_POST['Email']); 


//If not 0 duplicates (another one exists) create an error alert 
if(!mysqli_num_rows($checkDup->execute()) == 0){ 
    echo '<script language="javascript"> 
      alert("Email Already Exists"); 
      window.location.href = "Sign Up.php"; 
     </script>'; 
    unset($_POST); 
} 
+1

順便說一句,你應該使用'mysqli_num_rows($ checkDup->的execute())> 0',而不是'mysqli_num_rows($ resultDup)== 0' – rbr94

回答

0

您需要使用num_rows()

$checkDup = $db->prepare("SELECT Email FROM users WHERE Email= ?"); 
$checkDup->bind_param("s", $_POST['Email']); 
$checkDup->execute(); 
$checkDup->store_result(); 


if ($checkDup->num_rows > 0) { 

echo '<script language="javascript"> 
      alert("Email Already Exists"); 
      window.location.href = "Sign Up.php"; 
     </script>'; 
    unset($_POST); 
} 

參考:http://php.net/manual/en/mysqli-stmt.num-rows.php

+0

我覺得這是MySQLi,因爲當我嘗試你的代碼時,我得到「致命錯誤:未捕獲錯誤:調用未定義方法mysqli_stmt :: fetchColum」 –

+0

$ checkDup = $ db-> prepare(「SELECT Email FROM users WHERE Email =?」); $ checkDup-> bind_param(「s」,$ _POST ['Email']); $ checkDup-> execute(); $ checkDup-> store_result(); if($ checkDup-> num_rows> 0){ echo''; 未設置($ _ POST); } –

+0

非常感謝。我不知道你需要存儲結果。這似乎修復了它。 –