mysqli_prepare()在本地主机上运行的查询,但在线上传时不起作用,已准备好的语句中的变量

问题描述 投票:-1回答:1

我目前正在为客户网站建立商品清单清单的登录系统。

测试登录表单时,我一直在线获取这些错误:

警告:mysqli_stmt :: bind_param()[mysqli-stmt.bind-param]:变量数与第44行/home/greyan/public_html/shoplogin/login.php中已准备好的语句中的参数数不匹配] >

致命错误:在第48行的/home/greyan/public_html/shoplogin/login.php中调用未定义的方法mysqli_stmt :: get_result()。>

困扰我的是,这仅在我将其在线上传到Web服务器中时发生,而当我通过本地主机查看该网站时则不会发生。

//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()参数中所述,准备好的语句中的变量数似乎是正确的。

我目前正在为客户网站建立商品清单清单的登录系统。测试登录表单时,我一直在线获取这些错误:警告:mysqli_stmt :: bind_param()[mysqli -...

php mysql mysqli prepared-statement
1个回答
0
投票

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

为了安全起见,我还建议使用password_hash函数,因为您似乎很可能将密码存储为纯文本。

© www.soinside.com 2019 - 2024. All rights reserved.