简体   繁体   中英

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. 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.

My existing code is here - stuck on the bind_param step:

$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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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