increasing the quantity in cart when add the same product again.

Discussion in 'PHP' started by Kyriakos, May 21, 2008.

  1. #1
    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.
     

    Attached Files:

    Kyriakos, May 21, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    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:
     
    jestep, May 21, 2008 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    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.
     
    Kyriakos, May 21, 2008 IP