mysqli_stmt::bind_param()[mysqli stmt.bind param]:变量数与参数数不匹配

svgewumm  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(316)

我的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>

当我在没有加密密码的情况下测试表单时,它可以正常工作,因此当我尝试插入密码时,这一行会引起问题:

$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);

我应该把字符串改成别的密码吗?
谢谢

x33g5p2x

x33g5p2x1#

您的查询将采用的参数数由 ? 在您的查询中。
你有3个 ? 在您的查询中:

$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password) 
        VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';

你正在传入5个参数 bind_param :

$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(?))';

只传递3个参数 bind_param 功能:

$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
busg9geu

busg9geu2#

在这里传递四个变量:

$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ïu参数。您可能希望将sql查询替换为下一个查询:

$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
      VALUES(?, ?, ?, des_encrypt(substring(md5(?),1,8)))';
yduiuuwa

yduiuuwa3#

$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中传递一个变量。

相关问题