我是PHP的新手,还在学习,在编写PHP页面时遇到了这个问题,我认为第65行有错误,我确信逻辑很好。我想语法上有一些错误,你能帮我指出吗
该页面的目的是验证输入的电子邮件,检查是否存在于DB farmers中,如果存在,它将生成一个令牌并将令牌插入password_resets DB中,然后在链接中向用户发送带有令牌的电子邮件。
<?php
// Initialize the session
session_start();
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$email = "";
$email_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate new email
if(empty(trim($_POST["email"]))){
$email_err = "Please enter the email.";
} else{
$email = trim($_POST["email"]);
}
$sql = "SELECT email FROM farmers WHERE email = ?";
// Check input errors before updating the database
if($stmt = $mysqli->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("s", $param_email);
// Set parameters
$param_email = $email;
// Attempt to execute the prepared statement
if($stmt->execute()){
// Store result
$stmt->store_result();
// Check if email exists
if($stmt->num_rows == 1){
// generate a unique random token of length 100
$token = bin2hex(random_bytes(50));
$sql = "INSERT INTO password_reset(email, token) VALUES (?, ?)";
// Bind result variables
if($stmt = $mysqli->prepare($sql)){
$stmt->bind_param("ss", $param_email, $param_token);
// Set parameters
$param_email = $email;
$param_token = $token;
if($stmt->execute()){
// Send email to user with the token in a link they can click on
$to = $email;
$subject = "Reset your password on site.in";
$msg = "Hi there, click on this <a href="pwdreset-farmer.php?token=" . $token . "">link</a> to reset your password on our site";
$msg = wordwrap($msg,70);
$headers = "From: info@site.in";
mail($to, $subject, $msg, $headers);
header('location: pending.php?email=' . $email);
}
else{
echo "Oops! Something went wrong. Please try again later.";
}
}
else{
echo "Oops! Something went wrong. Please try again later.";
}
}
else{
// Display an error message if email doesn't exist
$email_err = "No account found with that email.";
}
// Close statement
$stmt->close();
}
}
// Close connection
$mysqli->close();
}
?>
PHP致命错误:未捕获错误:调用上的成员函数close((/var/www/html/pwdrstmail.php中的布尔值:78\n堆栈跟踪:\n#0{main}\n在第78行的/var/www/html/pwdrstmail.php中,它是$stmt->close((;
您使用数据库对象$stmt
两次,用于两个数据库查询。
第一个查询执行成功,但第二个查询可能失败,当您在第if($stmt = $mysqli->prepare($sql)){...
行重新分配它时,返回布尔值false
。
您看到的错误(如上所述(是因为在发生这种情况后调用$stmt->close()
,此时$stmt
等于false
,不再是可以关闭的数据库对象。