1

这是我第一次使用准备好的语句,我遇到了问题。当我运行以下代码时,我收到一条错误消息,内容如下:

警告:mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]:类型定义字符串中的元素数与第 49 行中的绑定变量数不匹配

第 49 行是下面的 mysqli_stmt_bind_param 语句。似乎字符串的数量(“ssssss”)与该语句中正确的字符串数量相匹配,所以我有点不知所措。

<?php

$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
$var4 = $_POST['var4'];
$var5 = $_POST['var5'];
$var6 = $_POST['var6'];

      if (!empty($var1)&&!empty($var2)&&!empty($var3)
      &&isset($var4, $var5, $var6));

      require_once 'connect.inc.php';

      $query = "INSERT INTO tablename (var1, var2, var3, var4, var5, var6)
      VALUES ('$var1','$var2','$var3','$var4','$var5', '$var6')";

      $stmt = mysqli_prepare($link, $query);

      mysqli_stmt_bind_param($stmt, "ssssss", $var1, $var2, $var3, $var4, $var5, 
      $var6);

      mysqli_stmt_execute($stmt);

      if (mysqli_stmt_affected_rows($stmt)==1);

      mysqli_stmt_close($stmt);

      $result = mysqli_query($link, $query);

      if ($result) {
        echo 'Thank you for your submission.';
        }    
      else {
          echo 'We were unable to process your information.'.mysqli_error($link).'
          Please ensure all required fields were filled out.;
          }

      mysqli_close($link);
?>

任何帮助深表感谢!谢谢!顺便说一句,我确实收到了“谢谢您的提交”。信息。

4

2 回答 2

3

将您的查询更改为:

$query = "INSERT INTO tablename (var1, var2, var3, var4, var5, var6)
      VALUES (?,?,?,?,?, ?)";

告诉您为绑定设置了参数。

于 2013-07-27T00:02:24.410 回答
2

问题与格式不匹配,是您没有要绑定的参数。参数应?在查询中用作占位符;

$query = "INSERT INTO tablename (var1, var2, var3, var4, var5, var6)
          VALUES ('$var1','$var2','$var3','$var4','$var5', '$var6')";

应该

$query = "INSERT INTO tablename (var1, var2, var3, var4, var5, var6)
      VALUES (?, ?, ?, ?, ?, ?)";   // <-- six place holders for the parameters
于 2013-07-27T00:02:49.360 回答