php pdo:prepare() 负数变为正数
我正在使用准备好的语句将表单中的值插入到数据库中,其中一些值是负数。
# html
<input type="radio" name="opinion" value='-2' />
<input type="radio" name="opinion" value='-1' />
<input type="radio" name="opinion" value='0' />
<input type="radio" name="opinion" value='1' />
<input type="radio" name="opinion" value='2' />
# prepare
prepare("UPDATE $db.$dbt SET $question = :value WHERE `key` = :key;");
已采取的故障排除步骤:
• 我回显了表单中POST
ed 值,负数返回负数。
• 我将prepare()
中的语句复制到CLI 中,并手动将负值插入到prepare 插入正值的同一列中:UPDATE db.dbt SET 意见= '-1' WHERE key = '10101';
(实际 SQL 中出现的刻度线)
^ 这有效。
• 我在执行时打印了准备好的语句,并且我注意到这些值没有用引号引起来。 我认为这就是问题的根源。
我读到不应在 prepare()
中引用值,因此我想不出其他可做的事情...
编辑: prepare()< /code> 在循环内
foreach ( $pairs as $pair ) {
list($question , $answer) = explode('=', $pair);
try {
$record_data = $dbh->prepare("UPDATE $db.$dbt SET $question = :value WHERE `key` = :key;");
$record_data->bindParam(':value', $answer);
$record_data->bindParam(':key', $key);
$record_data->execute();
} catch(PDOException $e){};
} // end foreach
I'm using a prepared statement to insert values from a form into a Db, and some of the values are negative.
# html
<input type="radio" name="opinion" value='-2' />
<input type="radio" name="opinion" value='-1' />
<input type="radio" name="opinion" value='0' />
<input type="radio" name="opinion" value='1' />
<input type="radio" name="opinion" value='2' />
# prepare
prepare("UPDATE $db.$dbt SET $question = :value WHERE `key` = :key;");
Troubleshooting steps already taken:
• I echoed the POST
ed values from the form and negatives return negative.
• I copied the statement in the prepare()
into the CLI and manually inserted a negative value into the same column where prepare is inserting the positive value:UPDATE db.dbt SET opinion = '-1' WHERE key = '10101';
(tickmarks present in actual SQL)
^ This worked.
• I printed the prepared statements as they're executed, and I noticed that the values are not enclosed in quotes. I think this is where the issue is coming from.
I read that values should not be quoted in the prepare()
, so I can't think of anything else to do…
EDIT: The prepare()
is inside a loop
foreach ( $pairs as $pair ) {
list($question , $answer) = explode('=', $pair);
try {
$record_data = $dbh->prepare("UPDATE $db.$dbt SET $question = :value WHERE `key` = :key;");
$record_data->bindParam(':value', $answer);
$record_data->bindParam(':key', $key);
$record_data->execute();
} catch(PDOException $e){};
} // end foreach
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 PDO:
-bindParam 的默认数据类型是
PDO::PARAM_STR
If you are using PDO :-
Default data type for bindParam is
PDO::PARAM_STR