【问题标题】:Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement [duplicate]警告:mysqli_stmt::bind_param():变量数与准备好的语句中的参数数不匹配[重复]
【发布时间】:2015-12-30 22:18:09
【问题描述】:

我收到以下错误:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

我在绑定和执行准备语句时遇到问题。与数据库的连接已成功建立,它确实设法将其插入数据库中,初始值为?

下面是代码:

// Set up the query
$insert = "INSERT INTO record_user (ip,country,address,stack,skills,employment_type,city_selection,landing_time,submission_time,time_spent) 
VALUES ('?','?','?','?','?','?','?','?','?','?')";


// Prepare the statement
$insert = $con->prepare($insert);

// Bind the statement
$insert->bind_param("ssssssssss", $user_ip, $country, $location, $stack, $skills, $employment, $city, $landing_time, $submission_time, $time_spent);

// Execute the statement
$insert->execute();

// Close the statement connection
$insert->close();

// Close the database connection
$con->close();

【问题讨论】:

  • 不要将? 放在引号中。该框架为您完成所有转义

标签: php mysql sql-server mysqli


【解决方案1】:

去掉 ? 周围的引号并使用 10 个参数而不是 11 个

而不是这个:

$insert = "INSERT INTO record_user (ip,country,address,stack,skills,employment_type,city_selection,landing_time,submission_time,time_spent) 
VALUES ('?','?','?','?','?','?','?','?','?','?')";
$insert = $con->prepare($insert);
$insert->bind_param("ssssssssss", $user_ip, $country, $location, $stack, $skills, $employment, $city, $landing_time, $submission_time, $time_spent);

试试这个:

$insert = "INSERT INTO record_user (ip,country,address,stack,skills,employment_type,city_selection,landing_time,submission_time,time_spent)
VALUES (?,?,?,?,?,?,?,?,?,?)";
$insert = $con->prepare($insert);
$insert->bind_param("ssssssssss", $user_ip, $country, $location, $stack, $skills, $employment, $city, $landing_time, $submission_time, $time_spent);

【讨论】:

  • 感谢 10 年代发生了什么?
  • 啊!被视为参数的 10 秒(它们在上下文中)添加多达 11 个参数而不是预期的 10
  • 感谢您的解释。我现在收到此错误 Call to a member function bind_param() on a non-object in
  • 你为什么去掉ss...ss? MySQLi 的bind_param 要求php.net/manual/en/mysqli-stmt.bind-param.php
  • @Jon220:添加这个:if($insert === FALSE){ die($con->error); }
【解决方案2】:

您准备好的查询没有任何占位符。您正在向数据库中插入 10 个 literal ? 字符。

你需要去掉?s 周围的引号:

$insert = "INSERT INTO record_user (ip,country,address,stack,skills,employment_type,city_selection,landing_time,submission_time,time_spent)
    VALUES (?,?,?,?,?,?,?,?,?,?)";

【讨论】:

    猜你喜欢
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2016-10-08
    • 2015-05-17
    • 2013-05-27
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多