Brunodgk Posted February 22, 2017 Share Posted February 22, 2017 (edited) Hello, I started to make a PHP site with Mysql to create a store for some products. The person only adds 1 product in the cart, not being able to add it 2 times in it. Iin the cart.php, there is a place for the person to change the quantity according to the quantity of the product in the database stock. The problem is: when the person changes the quantity of the LAST product, the quantity changes to all other products in the cart, because the quantity is not linked to the respective "pro_id" (product ID from the quantity changed). The code in PHP when clicks on 'update cart' with the quantity defined by the person: <?php if(isset($_POST['update_cart'])){ $qty= $_POST['qty']; if($qty<> 0){ $get_qty = "select product_qty from products where product_id='$pro_id'"; $run_qty = mysqli_query($con,$get_qty); $row_qty = mysqli_fetch_array($run_qty); $max_qty = $row_qty['product_qty']; if($qty<=$max_qty){ $update_qty = "update cart set qty='$qty'"; $run_qty = mysqli_query($con, $update_qty); $_SESSION['qty'] = $qty; $total *= $qty; } else{ echo "<script>alert('The quantity chosen exceeded the quantity of the DataBase!');</script>"; } } else{ echo "<script>alert('To remove the product, select the checkbox for the product and update your cart.');</script>"; } } ?> Thanks for help. Edited February 22, 2017 by Brunodgk Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2017 Share Posted February 22, 2017 You need a WHERE clause in the cart update query so you can specify the correct cart and product within that cart UPDATE ...SET ... WHERE ... Quote Link to comment Share on other sites More sharing options...
Brunodgk Posted February 22, 2017 Author Share Posted February 22, 2017 (edited) Hi Barand, Thank you for the fast answer! I Appreciate it. I've done many things, i followed your answer, but i saw on the phpmyadmin that only the quantity(qty) of p_id = 1 changed . I'm new to PHP and Mysql, I want to learn and do this ecommerce, I've based on many tutorials, and I've come this far and intend to finish it. Thanks again for the help. <?php if(isset($_POST['update_cart'])){ $get_p_id = "select p_id from cart where ip_add='$ip'"; $run_p_id = mysqli_query($con, $get_p_id); $row_p_id = mysqli_fetch_array($run_p_id); $pcart_id = $row_p_id['p_id']; $qty= $_POST['qty']; if($qty<>0){ $get_cartp_qty = "select product_qty from products where product_id='$pro_id'"; $run_cartp_qty = mysqli_query($con,$get_cartp_qty); $row_cartp_qty = mysqli_fetch_array($run_cartp_qty); $max_qty = $row_cartp_qty['product_qty']; if($qty<=$max_qty){ $update_qty = "update cart set qty='$qty' where p_id='$pcart_id'"; $run_qty = mysqli_query($con, $update_qty); $_SESSION['qty'] = $qty; //comments of the other things i tried //$insert_qty_cart = "insert into cart (p_id,ip_add,qty) values ('$pcart_id','$ip','$qty')"; //$run_qty_cart = mysqli_query($con, $insert_qty_cart); $subtotal = $qty * $single_price; $total += $subtotal; } else{ echo "<script>alert('The quantity chosen exceeded the quantity of the DataBase!');</script>"; } } else{ echo "<script>alert('To remove the product, select the checkbox for the product and update your cart.');</script>"; } } ?> Edited February 22, 2017 by Brunodgk Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 23, 2017 Share Posted February 23, 2017 you cannot (successfully) use the ip address to identify the visitor, since several people can share the same ip address and an ip address can even change during a single visit to a site. to avoid writing a bunch of extra code and queries, for a database based cart, just store the data as though it is an order with a status of 'pending'. see the following post (and the entire thread it is part of) for more information - https://forums.phpfreaks.com/topic/302627-how-may-i-able-to-getpost-my-code-that-allow-to-retrievepost-data-to-payment-page-and-to-get-the-total-from-myorder-page/?hl=%2Bpending&do=findComment&comment=1539851 this linked to post lists the database tables you need to accomplish this. also, as mentioned in the linked to post/thread, the php PDO extension is simpler and more constant to use than the php mysqli extension, especially when using prepared queries, which you need to use to supply data values to your sql query statements, to prevent sql injection. lastly, the best advice i can give to help you solve your current problem is to define what you want the code and data to do before writing any code. for your update_cart form processing code, define what inputs you have or need, what processing you are going to do based on those inputs, and what result or output you are going to produce. your form processing code should just process the form data. it should not be responsible for producing any output on the page, other than error or success messages related to the processing of the form data. there's no reason your form processing code should be calculating a sub-total or a total (these should be part of the code producing the dynamic output for the page and are not part of the form processing code) and since your cart is stored in a database, there's no reason to be storing any quantity values in session variables. Quote Link to comment Share on other sites More sharing options...
Brunodgk Posted February 23, 2017 Author Share Posted February 23, 2017 (edited) Thank you for the advice! this is a small shop, that's why i used the IP to identify users, but like you said, this can give me serious problems, i need to change it... I'll read the link you posted, and study more about the PDO. I was following like 2 or 3 tutorials of ecommerce in internet, but all of them got some problems that i need upgrade it by my self, like valid/duplicated email, prevent sql injection, php injectin, xss attack, etc. My page functions.php got the mosts of the calcs, i was testing some things to implement later, at this point i stayed at the beginning. In last case i'll write every thing again. I got some products here to sell and i'm studing web languages to improve my curriculum too... in my university i only study C and Java or C#, i'm learning at home with internet, thanks you two again, the opinions and advice, i really appreciate it! Edited February 23, 2017 by Brunodgk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.