我有这段代码:
$checkDB = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($conn, $checkDB);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if (mysqli_num_rows($result) == 1) {
$errorEmail = 'This email is registered m8';
$mainError = true;
}
所以问题是它总是说电子邮件已经注册,即使它不是用于电子邮件检查(电子邮件检查是在名称检查之后)。我尝试更改变量名称,但它不起作用。
$checkDB = "SELECT user_name FROM users WHERE user_name='$name'";
$result = mysqli_query($conn, $checkDB);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if (mysqli_num_rows($result) == 1) {
$errorName = 'Name registered';
$mainError = true;
}
答案 0 :(得分:0)
mysqli_num_rows($result);
检查找到的电子邮件的行数。并且不要放mysqli_fetch_array($result, MYSQLI_ASSOC);
。我不知道为什么,但它对我有用。
使用此代码
$checkDB = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($conn, $checkDB);
$check = mysqli_num_rows($result);
if ( $check == 1) {
$errorEmail = 'This email is registered m8';
$mainError = true;
}
答案 1 :(得分:0)
$checkDB = "SELECT email FROM users WHERE email= ?";
if ($result = mysqli_prepare($conn, $checkDB)) {
mysqli_stmt_bind_param($result, "s", $email);
mysqli_stmt_execute($result );
if(mysqli_num_rows($result) > 0){
$errorName = 'Name registered';
$mainError = true;
}
}
mysqli_close($conn);