简体   繁体   English

MySQLi 更新查询 - 使用参数化查询增加值

[英]MySQLi update query - increment value with parameterized query

Essentially I'm just trying to increment a value in columnA by 1, but I want to do so with a parameterized mysqli query.本质上,我只是想将columnA中的值增加 1,但我想使用参数化的 mysqli 查询来这样做。 The existing value may be null, blank or a number, but if it is the former two, then the query should just treat it as 0 and increment it by 1. The example here does so without a parameter.现有值可能是 null、空白或数字,但如果是前两者,则查询应将其视为 0 并将其递增 1。 此处的示例没有参数。

My existing code is here - stuck on the bind_param step:我现有的代码在这里 - 停留在 bind_param 步骤:

$prepare = $this -> db -> prepare("UPDATE userinfo SET idarray = ?, currentkey = ? WHERE id = ?");
$prepare -> bind_param('sii', serialize($numbers), currentkey + 1, $ID);
$prepare -> execute();

if ($prepare -> errno)
{
    echo $prepare -> error;
}

$prepare -> close();

Why do you try to put a record value currentkey + 1 (non PHP variable) into your prepared statement?为什么要尝试将记录值currentkey + 1 (非 PHP 变量)放入准备好的语句中?

Can you just try:你可以试试:

//...
$serialized_numbers = serialize($numbers);
$prepare = $this->db->prepare("UPDATE userinfo SET idarray = ?, currentkey = currentkey + 1 WHERE id = ?");
$prepare->bind_param('si', $serialized_numbers, $ID);
$prepare->execute();
// ...

If you think about it your currentkey + 1 is not a parameter from PHP, your SQL engine knows the value and how to do the sum如果您考虑一下,您的currentkey + 1不是 PHP 的参数,您的 SQL 引擎知道该值以及如何求和

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM