gpt4 book ai didi

php - "Number of bound variables does not match number of tokens",但我每个 "?"都有一个参数

转载 作者:行者123 更新时间:2023-11-29 11:09:22 25 4
gpt4 key购买 nike

我正在尝试将 POST 提交的结果插入 MySQL 数据库。

<?php
if(isset($_POST['activity'])&&isset($_POST['timeLength'])&&isset($_POST['datevalue']))
{
ini_set('display_errors', 1);
$name=$_POST['activity'];
$timeLength=$_POST['timeLength'];
$datevalue=$_POST['datevalue'];
$datevalue=date("m-d-Y",strtotime($datevalue));
echo $name." ".$timeLength." ".$datevalue;
if(strlen(trim($timeLength))>0)
{
$dsn="mysql:host=database.net;dbname=databasename;charset=utf8";
$db=new PDO($dsn,'databasename','password');
$insertSQL="insert into preasc_activityLog (name,timeLength,datevalue) values (?,?,?)";
$stmt=$db->prepare($insertSQL);
$stmt->bindParam("sis",$name,$timeLength,$datevalue);
$stmt->execute();
}
}
else
{
echo "FAIL!";
}
?>

根据我的测试数据,echo()调用返回:

program 2 11-22-2016

表中要插入的字段具有各自的数据类型 char(40) , mediumint ,和datetime ,所以数据看起来应该适合 INSERT 语句。

但是我得到以下错误结果:

<b>Notice</b>:  A non well formed numeric value encountered in 
<b>/home/server/module/submit.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter
number: number of bound variables does not match number of tokens in
<b>/home/server/module/submit.php</b> on line <b>17</b><br />

日期似乎应该是可以接受的,但它导致了“格式不正确”错误(因为如果我只使用前两个参数,此错误就会消失)。

$insertSQL中有3个代币,与 bindParam() 中的 3 个参数匹配称呼。那么 token 和绑定(bind)变量之间的不一致在哪里呢?

最佳答案

你不能这样使用bindParam。您必须为每个参数调用一次,或者使用execute。而且拼写错误。

$sth = $dbh->prepare('SELECT nom, couleur, calories
FROM fruit
WHERE calories < ? AND couleur = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $couleur, PDO::PARAM_STR, 12);

关于php - "Number of bound variables does not match number of tokens",但我每个 "?"都有一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40882048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com