mysqli_prepare() 查询在本地主机上工作但在线上传时不工作,准备语句上的变量

mysqli_prepare() Query working on localhost but not when uploaded online, variables on prepared statements

我目前正在为一个客户的网站制作商品清单的登录系统。

我在测试登录表单时总是在线收到这些错误:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in /home/greyan/public_html/shoplogin/login.php on line 44

Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/greyan/public_html/shoplogin/login.php on line 48

让我烦恼的是,只有当我将它在线上传到网络服务器时才会发生这种情况,而当我通过本地主机查看网站时不会发生这种情况。

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = trim($str);
global $conn;
return $conn->real_escape_string($str);
}


//Sanitize the POST values
$username = clean($_POST['username']);
$password = clean($_POST['password']);

//Input Validations
if($username == '') {
$errmsg_arr[] = '*Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = '*Password missing';
$errflag = true;
}

//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.php");
exit();
}

$sql = $conn->prepare("SELECT * FROM users WHERE username = '$username' AND password = '$password' ") ;
$sql->bind_param("ss", $username, $password) ;

$sql->execute();

$result = $sql->get_result();

我真的很困惑为什么这些错误不断发生。正如 bind_param() 参数中所述,准备好的语句中的变量数量似乎是正确的。

不要转义您的字符串并将变量名称“$username”和“$password”替换为问号 (?)。另外,不要使用单引号。

作为安全预防措施,我还建议使用 password_hash 函数,因为您可能会将密码存储为纯文本。