I am trying to increment rows in a table using PHP PDO and i have come up with this query
UPDATE users SET log = ?
I am trying to make an update based on the previous value on each column of the log
So, if my table was like this earlier
+----+------+------+-----------+--------+
| id | name | age | eye_color | log |
+----+------+------+-----------+--------+
| 21 | OLa | 19 | black | 1 |
| 22 | OLa | 19 | Green | 2 |
| 23 | OLa | 19 | Grey | 3 |
+----+------+------+-----------+--------+
Am expecting to get this result
+----+------+------+-----------+--------+
| id | name | age | eye_color | log |
+----+------+------+-----------+--------+
| 21 | OLa | 19 | black | 2 |
| 22 | OLa | 19 | Green | 3 |
| 23 | OLa | 19 | Grey | 4 |
+----+------+------+-----------+--------+
I found out i could make the value of my placeholder to be
log + 1
There by making the full query
UPDATE users SET log = log + 1
This works well when i use the PDO's query method and also from my terminal, the problem comes when I try updating this using prepared statements, If i did
$stmt = $this->pdo->prepare("UPDATE users SET log = ?");
$stml->execute(['height + 1']);
Then all the log columns becomes 0.
Is there anything am doing wrong? I also know i can just make the query plain
$stmt = $this->pdo->prepare("UPDATE users SET log = log + 1");
$stmt->execute(['log + 1']);
But i would prefer my initial approach, i am working with some constraints.
height + 1what is height supposed to be? With the single quotes around it you are passing a literal string to the update which is not what you intended.UPDATE users SET log = height + ?and$stml->execute(['1']);$stmtvs$stmlheight + 1then why does your code showlog + 1