0

我无法插入数据条目。我花了几个小时,查看各种教程和示例代码,但我无法弄清楚为什么以下内容不起作用:

$data1 = 0;
$data2 = "pumpkin";
$data3 = "cake";
$data4 = "orange";
$data5 = "sweet";
$stmt = $mysqli->stmt_init();
// Here I have a few SELECT queries that checks if $data2 and/or $data3 is already taken
$query = "INSERT INTO ". $mytable . " SET data1=?, data2=?, data3=?, data4=?, data5=?";
if ($stmt->prepare($query)) {
    $stmt->bind_param("issss", $data1, $data2, $data3, $data4, $data5);
    $stmt->execute() or trigger_error($mysqli->error); // added per Your Common Sense's suggestion
    //$stmt->bind_result($data1, $data2, $data3, $data4, $data5);
    $stmt->close();
    $mysqli->close();
} else {
    echo $mysqli->error; // edited per Yogesh Suthar's suggestion
    $stmt->close();
    $mysqli->close();
}

我在网上遇到错误$stmt->bind_result

警告:mysqli_stmt::bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配

我究竟做错了什么?

我在 $db_user、$db_password、$db_dbname 上正确定义了以下内容:

$mysqli = new mysqli('localhost', $db_user, $db_password, $db_dbname);
4

2 回答 2

0

删除此行,因为您正在触发INSERT查询并且它返回受影响的行数。您必须bind_result仅用于SELECT查询。

$stmt->bind_result($data1, $data2, $data3, $data4, $data5);

用这个

echo $mysqli->error;

代替

echo "failed";
于 2013-02-19T05:52:06.567 回答
-1

如果未插入数据,则出现错误。
所以你必须向mysql询问这个错误

$stmt->execute() or trigger_error($mysqli->error);

因此,您将收到某个错误的通知,无需猜测。

于 2013-02-19T06:23:38.730 回答