papaface Posted December 15, 2006 Share Posted December 15, 2006 Hello,I was wondering if someone can help me.I am trying to make a kind of shopping cart.A person submits a form of checkboxes with each items quantity equaling "1".How would I go about checking in the mysql database if the name of that item exists in the order table, and if so increment the current quantity stored in the table by one.On the other hand if the value is not present, add it with the quantity equalling 1.Hope you can understand that. I would appreciate any help anyone can give.Thanks.edit:The code I currently have in a script named process.php is:[code]if (isset($_POST["submit"])) { foreach ($_POST as $key => $value) { if ($key != "submit") { mysql_query("insert into orders (order_item) values ('$value')",$con) or die (mysql_error()); } } }[/code] Link to comment https://forums.phpfreaks.com/topic/30759-help-with-phpmysql-if-a-value-exists-do-something/ Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 $query = mysql_query("SELECT * FROM orders WHERE order_item = '$value'");$check = mysql_num_rows($query);if(!$check){ mysql_query("insert into orders (order_item) values ('$value')",$con);} else { $query = mysql_query("SELECT quantity FROM orders WHERE order_item='$value'"); $data = mysql_fetch_assoc($query); $quantity = $data["quantity"]; $quanitity = $quantity + 1; mysql_query("UPDATE orders SET quantity=$quantity WHERE order_item='$value'");}That should work. Link to comment https://forums.phpfreaks.com/topic/30759-help-with-phpmysql-if-a-value-exists-do-something/#findComment-141782 Share on other sites More sharing options...
roopurt18 Posted December 15, 2006 Share Posted December 15, 2006 If you have MySQL 4.1+ you can use INSERT ... UPDATEhttp://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html Link to comment https://forums.phpfreaks.com/topic/30759-help-with-phpmysql-if-a-value-exists-do-something/#findComment-141901 Share on other sites More sharing options...
papaface Posted December 15, 2006 Author Share Posted December 15, 2006 Thanks for your help. :) Link to comment https://forums.phpfreaks.com/topic/30759-help-with-phpmysql-if-a-value-exists-do-something/#findComment-141998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.