[英]Mysqli:mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement
[英]mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters
我的 php 表单在我的表中插入了几列和一个加密的密码。 但是,当我运行它时,它说变量数与参数数不匹配。 这是我的代码:
<?php
if (isset($_POST['insert'])) {
require_once 'login.php';
$OK = false;
$conn = new mysqli ($host, $user, $password, $database) or die("Connection Failed");
$stmt = $conn->stmt_init();
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
if ($stmt->prepare($sql)) {
// bind parameters and execute statement
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
// execute and get number of affected rows
$stmt->execute();
if ($stmt->affected_rows > 0) {
$OK = true;
}
}
if ($OK) {
header('Location: confirm.php');
exit;
} else {
$error = $stmt->error;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<?php if (isset($error)) {
echo "<p>Error: $error</p>";
} ?>
<form id="form1" method="post" action="">
<p>
<label for="user_email">User email:</label>
<input name="user_email" type="text" class="widebox" id="user_email">
</p>
<p>
<label for="user_name">User name:</label>
<input name="user_name" type="text" class="widebox" id="user_name">
</p>
<p>
User role: <select name = "user_pref">
<option value = "BLU">Blue</option>
<option value = "YEL">Yellow<option>
<option value = "GRE">GREEN</option>
</select>
</p>
<p>
<input type="submit" name="insert" value="Register New User" id="insert">
</p>
</form>
</body>
</html>
当我在没有 ENCRYPTED PASSWORD 的情况下测试表单时,它工作正常,因此当我尝试插入密码时,此行会导致问题:
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
我应该将字符串更改为其他密码吗?
谢谢
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
仅定义 3 个占位符,但您尝试写入 4 个。
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
对于每个 ? 你在准备好的 SQL 语句中插入你必须在 bind_param 中传递一个变量。
你在这里传递四个变量:
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
但只需要其中三个
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
看 ”?” 标记,它将被 bild_params 替换。 您可能想将 SQL 查询替换为下一个查询:
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(?),1,8)))';
您的查询要采用的参数数量由?
在您的查询中。
你有 3 ?
在您的查询中:
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
并且您在bind_param
中传递了 5 个参数:
$stmt->bind_param($_POST['user_email'], $_POST['user_name'], $_POST['user_pref']);
有两种可能的解决方案:
在查询中取 5 个参数:
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password) VALUES(?, ?, ?, ?, des_encrypt(?))';
在bind_param
函数中只传递 3 个参数:
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.