可能重复:
It cannot find the correct email address in the database
我的if else语句有问题。当我在文本框中输入正确的用户名和电子邮件时,它会继续执行else语句和回声"您输入了错误的电子邮件地址"而不是执行if语句并回显"您的详细信息是正确的"。有没有人知道为什么它回应了错误的陈述,即使我输入了正确的用户名并从数据库中更正了与该用户相关的电子邮件?
if (isset($_POST['resetbtn'])) {
//get form data
$user = $_POST['user'];
$email = $_POST['email'];
//make sure info is provided
if ($user) {
if ($email) {
if ((strlen($email) >= 7) && (strstr($email, "@")) && (strstr($email, "."))) {
$query = "SELECT TeacherUsername, TeacherEmail FROM Teacher WHERE TeacherUsername = ?";
// prepare query
$stmt = $mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s", $user);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherUsername, $dbTeacherEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
if ($numrows == 1) {
$row = $stmt->fetch();
$dbTeacherEmail = $row['TeacherEmail'];
//make sure email is correct
if ($email == $dbTeacherEmail) {
echo "Your details are correct";
} else {
echo "You entered in the Wrong Email Address";
}
} else {
echo "Please Enter in your Email";
}
} else {
echo "Please Enter in your Username";
}
}
}
}
答案 0 :(得分:1)
为了比较php中的字符串,请不要使用==。
使用
strcmp($string1,$string2)==0
测试平等。
答案 1 :(得分:0)
我认为您错误地使用了$stmt->fetch()
。您似乎将返回值视为数组,但返回值是布尔值,$dbTeacherEmail
的值已经$stmt->bind_result($dbTeacherUsername, $dbTeacherEmail)
绑定到结果。
尝试删除读取的行
$dbTeacherEmail = $row['TeacherEmail'];
看看你是否得到了你想要的东西。另请参阅mysqli-stmt::fetch()
上的文档