A litle help with shopping cart

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

  1. #1
    hi,

    i have a shopping cart with this SQL queries:
    <?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 want to display a message when the visitor adds a product for second time in the cart. when the product is already in the cart i want to display a message like that: "the product is already in the cart"

    can anybody give a litle help?

    thanks in advance.
     
    Kyriakos, May 20, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    <?phpsession_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; ?>
    if ($cart=="add")
    {
    
    $result2=mysql_query("SELECT * FROM cart WHERE sessionid='".$sessionid."' ORDER BY pricer");
    $tt=0;
    while ($row = mysql_fetch_array($result2)) { 
    if ($row["code"]==$code)
    {
    $tt=1;
    
    }
    
    } //while
    if ($tt==1)
    echo "product allready added";
    
    }
    
    <?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) ) 
    { 
    if($tt==0)
       $res= mysql_query($sql);   
     // handle any query errors here 
     }
    }
    $result = mysql_query("SELECT * FROM cart WHERE sessionid='".$sessionid."' ORDER BY pricer");
    mysql_close($con);
    ?>
    Code (markup):
     
    kmap, May 21, 2008 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    you are the man. thank you very match.
     
    Kyriakos, May 21, 2008 IP