当前位置: 首页 > 知识库问答 >
问题:

MySQLi准备的语句变量数与参数数不匹配[重复]

顾乐心
2023-03-14

我正在使用mysqli编写我认为是相当基本的准备好的状态。语句如下所示(其中$connectionMySQLI实例):

    $venue = $_POST['venue'];
    $description = $_POST['description'];

    if (isset($_POST['add'])) {
       $query = $connection->prepare("INSERT INTO events (date, time, showTime, venue, description) VALUES ('$date', '$time', '$showtime', '?', '?')");
       $query->bind_param('ss', $venue, $description);
    }
    elseif (isset($_POST['edit'])) {
       $query = $connection->prepare("UPDATE events SET date = '$date', time = '$time', showTime = '$showtime', venue = '?', description = '?' WHERE id ='$id'");
       $query->bind_param('ss', $venue, $description);
    }

    $query->execute();

这将失败,在日志中出现以下情况:变量数与准备好的语句中的参数数不匹配。

共有1个答案

颜安宁
2023-03-14

在没有详细说明的情况下,请不要将“直接值”与值持有者(?)在你的查询中。相反,一次将它们全部绑定。尝试以下操作:

$query = $connection->prepare("INSERT INTO events (date, time, showTime, venue, description) VALUES (?, ?, ?, ?, ?)");
$query->bind_param('sssss', $date, $time, $showtime, $venue, $description);
 类似资料: