2015-03-02 107 views
0

我不知道爲什麼我的代碼似乎沒有工作。我想檢查數據庫中是否存在電子郵件,如果不存在,請繼續註冊。代碼如下:PHP準備好的語句 - MySQL檢查用戶是否存在

if (empty($errors)) { //Using Prepared Statements 
// Connect to the database: 
$dbc = mysqli_connect ('localhost','root', 'pass', 'book_store'); 
$q = "SELECT user_id FROM users WHERE email=?"; 
$stmt = mysqli_prepare($dbc, $q); 
mysqli_stmt_bind_param($stmt, 'i', $email); 
    mysqli_stmt_execute($stmt); 
$result = mysqli_stmt_get_result($stmt); 
$num_rows = mysqli_num_rows($result); 


if ($num_rows == 0) { //Check if email exists 
$q = 'INSERT INTO users(first_name, last_name, state, email) VALUES (?, ?, ?,  ?)'; 
$stmt = mysqli_prepare($dbc, $q); 
mysqli_stmt_bind_param($stmt,'ssss', $fn, $ln,$state, $email); 
mysqli_stmt_execute($stmt); 

// Closee statement: 
mysqli_stmt_close($stmt); 

// Close the connection: 
    mysqli_close($dbc); 

} else { 
echo '<h1>email exists</h1>'; 
} 
} 
else { 
echo '<p>The Errors Occurred:<br />'; 
foreach ($errors as $msg) { 
    echo " - $msg<br />\n"; 
} 
echo '</p><p>Please Try Again.</p>'; 
} 
} 
+0

'似乎沒有工作 - 請定義這個。 – sectus 2015-03-02 05:43:23

回答

2

您已經給出i,它代表int類型的變量。嘗試使用s來替換它,如下所示。 $q = "SELECT user_id FROM users WHERE email=?"; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 's', $email);

+0

它的工作!該死的我浪費了4個小時... – Sam 2015-03-02 06:00:39

+0

如果有效,請接受答案。 – Shameer 2015-03-02 06:49:01