Hello everyone.
In the task, you need to display the product in a cycle and inside the information about each product there is a counter, which on click increases the product by 1. When I click on one product, the value of all products changes. I do not understand how to make a condition to act only for one product. Below is how I do (sorry if I format it incorrectly):

<?php $dbconnect; $var_order_id = (int)$_GET['ordid']; $c_result = pg_query("SELECT i.image, i.name, c.prod_price, c.prod_count, c.id_cart FROM cart as c join items as i on i.id = c.prod_id WHERE c.order_id = '$var_order_id'"); $div_numc = pg_num_rows($c_result); for($i=0; $i < $div_numc; $i++){ $cart_catalog = pg_fetch_array($c_result); $id_cart = $cart_catalog['id_cart']; //При клике на кнопку делаю вот этот апдейт. if(isset($_POST['subplus'])){ $update_query = pg_query("UPDATE cart SET prod_count = prod_count+1, sum_price = prod_price*(prod_count+1) WHERE id_cart = '$id_cart' returning prod_count"); $v_prod_count = pg_fetch_array($update_query); $var_prod_count = $v_prod_count['prod_count']; } ?> 

Here is the html with what I print.

 <?php } ?> 
  • and you exactly when "UPDATE cart SET prod_count = prod_count + 1, sum_price = prod_price * (prod_count + 1) WHERE id_cart = '$ id_cart' returning prod_count" change one? You just try to choose according to this condition and see what will happen - jashka
  • you had better transfer the item id to your $ _POST and your subplus - jashka
  • jashka, forgive me for incompetence, did not understand a little. I try to click on the counter of one product, but it changes for everyone if I put a request inside the cycle. If after the cycle, then only the last record changes, no matter what it clicks (( - upscale
  • jashka, subplus - this is my submit, I pass it through the post. I tried to add the product id to the request condition. All the same changes at all. - lihim

1 answer 1

In your code, you loop around all the items in the basket and, if the flag you need is in $ _POST, increase the quantity. Every item. That is the problem. You need to transfer the product id to the server that you want to update and either select only this basket item if the rest of your code is not needed, or check not only the flag within the loop, but also the coincidence id:

 if (isset($_POST['subplus']) && $_POST['item_id'] == $id_cart) {} 
  • I added an example of how this should look like on the server side. - Alexey Ukolov
  • I tried to send an id with a hidden input when clicked, but it is sent from the cycle, therefore, it still applies to all the goods in the basket ((( lihim
  • Show your code. - Alexey Ukolov
  • And where is your form itself? You would learn the basics of html ... Each product must have its own form. - Alexey Ukolov
  • Probably you can, I do not really understand what you are talking about. Please do not need to make a chat in the comments: if you have a new question - issue it in the form of a full question. Regarding the current question - I can not help you, because I do not see all the relevant code. You can have anything there. - Alexey Ukolov