'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();
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
