PHP准备的语句-MySQL检查用户是否存在
我不知道为什么我的代码似乎不起作用。我想检查数据库中是否存在电子邮件,如果不存在,请继续注册。代码如下:PHP准备的语句-MySQL检查用户是否存在,php,Php,我不知道为什么我的代码似乎不起作用。我想检查数据库中是否存在电子邮件,如果不存在,请继续注册。代码如下: 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=?"; $s
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>';
}
}
if(空($errors)){//使用准备好的语句
//连接到数据库:
$dbc=mysqli_connect('localhost','root','pass','book_store');
$q=“从电子邮件=?”的用户中选择用户标识”;
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,$i',$email);
mysqli_stmt_execute($stmt);
$result=mysqli\u stmt\u get\u result($stmt);
$num_rows=mysqli_num_rows($result);
如果($num_rows==0){//检查电子邮件是否存在
$q='在用户(名字、姓氏、状态、电子邮件)中插入值(?、、?、?);
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'ssss',$fn,$ln,$state,$email);
mysqli_stmt_execute($stmt);
//Closee声明:
mysqli_stmt_close($stmt);
//关闭连接:
mysqli_close($dbc);
}否则{
回音“电子邮件存在”;
}
}
否则{
echo'发生错误:
;
foreach($msg错误){
回显“-$msg
\n”;
}
回显“请重试。”;
}
}
您已经给出了表示int类型变量的i
。请尝试用s
替换它,如下所示。
$q=“从电子邮件=?”的用户中选择用户标识”;
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,$s',$email);
似乎不起作用
——请定义一下。