所以我试图从URL(http://example.com/pb.php?id=123&affiliate=abd123&lp1=dun.com&lp2=dun2.com&lp3=dun3.com)中获取变量,我尝试过这段代码,但是收到了这个错误
准备失败:(1136)列数与第1行的值计数不匹配 致命错误:在第25行的/home/recondes/public_html/postback.php中调用boolean上的成员函数bind_param()
以及
<?php
define("MYSQL_HOST", "localhost");
define("MYSQL_PORT", "3306");
define("MYSQL_DB", "db");
define("MYSQL_TABLE", "tbl");
define("MYSQL_USER", "user");
define("MYSQL_PASS", "pass");
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$id = $_GET['id'];
$affiliate = $_GET['affiliate'];
$lp1 = $_GET['lp1'];
$lp2 = $_GET['lp2'];
$lp3 = $_GET['lp3'];
if (!($stmt = $mysqli->prepare("INSERT INTO ".MYSQL_TABLE." VALUES (id, affiliate, lp1, lp2, lp3);")))
{
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt->bind_param('dds', $id, $affiliate, $lp1, $lp2, $lp3 );
if (!$stmt->execute())
{
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
{
printf("%d Row updated, added ".$id." to ".$affiliate." .\n", mysqli_stmt_affected_rows($stmt));
}
?>
答案 0 :(得分:0)
您的查询未列出要插入的列,因此它希望您提供所有表列的值。您尚未显示表架构,但它不只有5列。
您还错过了bind_param()
填写的占位符。我怀疑您在VALUES()
中列出的值是表格列。所以试试:
if (!($stmt = $mysqli->prepare("INSERT INTO ".MYSQL_TABLE." (id, affiliate, lp1, lp2, lp3) VALUES (?, ?, ?, ?, ?)")))
此外,在对bind_param
的调用中,指定数据类型的字符串需要包含与参数一样多的字母。所以它应该是:
$stmt->bind_param('dssss', $id, $affiliate, $lp1, $lp2, $lp3 );
最后,当您在一个步骤中出现错误并打印错误消息时,应该停止此脚本而不是继续执行下一步。如果prepare()
失败,则使用预准备语句是没有意义的。