1

I have a problem with my incrementation in php.

I try to add a button "+" who add +1 when I Click on the button for my ingredients.

So for example, If I click on + for "tomatoes" and If in my database I had 50 tomatoes. Now I have 51 tomatoes

I did it, but the problem is that when I click on "+", it add me not +1 but +8 ( Maybe because I have 8 ingredients in my database)

Here my code :


$nom = 'salade';
$req=$db->prepare("UPDATE ingredients SET stock = stock +1 WHERE nom =:nom");
$req->execute(array('nom' =>$nom));

enter image description here

Thanks for reading me !

jacques
  • 23
  • 5
  • 1
    If you have multiple rows that meet your `WHERE` clause, then yes, all of those rows will be updated. – Patrick Q Apr 14 '20 at 12:22
  • No, I try just for 'salade' I try add +1 just for salade, but in salade it add me +8 and not +1 – jacques Apr 14 '20 at 12:24
  • Then you have 8 rows containing salade or you click many times. You could add LIMIT 1 to the query or better lock the button. – Markus Zeller Apr 14 '20 at 12:25
  • no, I edit my post – jacques Apr 14 '20 at 12:29
  • just when I clikc on the button stock, It put me "+8" and not "+1" for salade – jacques Apr 14 '20 at 12:31
  • Even if there are multiple matching rows, I can't see how this code can possibly add 8 to anything unless it's running in a loop or the form is being submitted multiple times. Can you post your full HTML and PHP code? – iainn Apr 14 '20 at 12:31
  • 1
    @iainn Well, if there were 8 matching rows, adding 1 to each, and OP was doing a `SUM` when querying, that would do it. But I agree, we need more information. Something's going on that we're not being shown. – Patrick Q Apr 14 '20 at 12:33
  • yes , it's good it works now, thx !! But I would like know, Why when I do F5 it refresh me the page, but It add me too +1 ??? – jacques Apr 14 '20 at 13:13

0 Answers0