hi everybody, i want to increase the product quantity by 1 when the customer adds the same product again in the cart. this is my PHP code: <?php include("connection.php"); ?> <?php session_start(); $sessionid = session_id(); $code = $_POST["code"]; $model = $_POST["model"]; $descr = $_POST["descr"]; $pricer = $_POST["pricer"]; $qty = $_POST["qty"]; $cart = $_POST["cart"]; If($qty == "") $qty=1; ?> <?php if ( !empty($cart) ) { switch($cart) { case "add": $sql= "INSERT INTO cart (sessionid, code, model, descr, pricer, qty) VALUES ('$sessionid','$code','$model','$descr','$pricer','$qty')"; break; case "del": $sql= "DELETE FROM cart WHERE code='$code'"; break; case "upd": $sql= "UPDATE cart SET qty='$qty' WHERE code='$code'"; break; } if ( !empty($sql) ) { $res= mysql_query($sql); // handle any query errors here } } $result = mysql_query("SELECT * FROM cart WHERE sessionid='".$sessionid."' ORDER BY pricer"); mysql_close($con); ?> PHP: i have attached the cart.php if anybody wants to take a look. some words are in greek. just ignore it. thanks in advance.
If you want a quick hack, try this: session_start(); $sessionid = session_id(); $code = $_POST["code"]; $model = $_POST["model"]; $descr = $_POST["descr"]; $pricer = $_POST["pricer"]; $qty = $_POST["qty"]; $cart = $_POST["cart"]; If($qty == "") $qty=1; ?> //NEW UPDATE HACK $if_ud_query = mysql_query("SELECT qty FROM cart WHERE sessionid = '$sessionid' AND code = '$code'"); if(mysql_num_rows($if_ud_query) > 0) { //the item is already in the cart $cart = 'upd'; } //END NEW UPDATE HACK <?php if ( !empty($cart) ) { switch($cart) { case "add": $sql= "INSERT INTO cart (sessionid, code, model, descr, pricer, qty) VALUES ('$sessionid','$code','$model','$descr','$pricer','$qty')"; break; case "del": $sql= "DELETE FROM cart WHERE code='$code'"; break; case "upd": $sql= "UPDATE cart SET qty='$qty' WHERE code='$code'"; break; } PHP:
it doesn't work my friend. when i add the same product again, the quantity isn't increased. the update and delete sql queries is not working. only the insert query is working.