searls03 Posted February 4, 2012 Share Posted February 4, 2012 what I would like to do is have a query that if $product_id with specific $cart_id is already present in database, then it will update quantity instead of inserting a new row, but if $product_id is not present with specific $cart_id(important that it isn't with this) then it will insert a new row. can anyone help? <?php include_once("connect.php"); session_start(); if($_POST['submit']) { // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM cart where cart_id = ".$_SESSION['cart_id'].""); while($row = mysql_fetch_array($sql)){ $product = $row["product123"]; $price1 = $row["price"]; $id = $row["product_id"]; $qty = $row["quantity"]; } $product = mysql_real_escape_string($_POST['hiddenField']); $price = mysql_real_escape_string($_POST['hiddenField1']); $id = mysql_real_escape_string($_POST['hiddenField2']); $sql = "INSERT INTO cart (price, product123, quantity, cart_id, product_id) VALUES('$price', '$product', '1', '".$_SESSION['cart_id']."', '$id' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256388-association/ Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2012 Share Posted February 4, 2012 INSERT . . . ON DUPLICATE KEY UPDATE Quote Link to comment https://forums.phpfreaks.com/topic/256388-association/#findComment-1314416 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 but the problem I have is associating the two with each other.....that is the main thing I need......... Quote Link to comment https://forums.phpfreaks.com/topic/256388-association/#findComment-1314418 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.