SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If statement Help

    I am trying to make a script so that if a product already exits in a shopping cart instead of adding it again so it appears twice it updates the current product in the shopping cart with the new quantity.

    I have this bit working the problem lies with the fact that the else part of the statement isn't working, so if the product doesn't exist in the cart it won't add it.

    Any help appreciated

    Code:
    <?php
    session_id();
    session_start();
    include("database/database.php");
    $qty = $_POST['qty'];
    $prodnum = $_POST['prodnum'];
    $sess = session_id();
    
    if (empty($qty) || !is_numeric($qty))
    {include("error.php");
    }
    
    $query = "SELECT * from carttemp WHERE sess = '$sess'";
    $results = mysql_query($query) or die(mysql_error());
    $numrows = mysql_num_rows($results);
    
    	if ($numrows == 1)
    	{while ($row = mysql_fetch_array($results)) {
    	$prodnumexists = $row[prodnum];
    	$currentquan = $row[quan];
    		if ($prodnumexists == $prodnum)
    		{$updateqty = $currentquan + $qty;
    		mysql_query("UPDATE carttemp SET quan = '$updateqty' WHERE prodnum = '$prodnum'");
    		}}}
    else {
    $query1 = "INSERT INTO carttemp (sess, quan, prodnum) VALUES ('$sess','$qty','$prodnum')";
    $results1 = mysql_query($query1) or die(mysql_error());
    }
    include ("cart.php");
    exit;
    ?>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I changed the indentation of your 'if':
    PHP Code:
      if ($numrows == 1) {
        while (
    $row mysql_fetch_array($results)) {
          
    $prodnumexists $row[prodnum];
          
    $currentquan $row[quan];
          if (
    $prodnumexists == $prodnum) {
            
    $updateqty $currentquan $qty;
            
    mysql_query("UPDATE carttemp SET quan = '$updateqty' WHERE prodnum = '$prodnum'");
          }
        }
      }
      else 
      {
        
    $query1 "INSERT INTO carttemp (sess, quan, prodnum) VALUES ('$sess','$qty','$prodnum')";
        
    $results1 mysql_query($query1) or die(mysql_error());
      } 
    As you can see, if the number of lines found is equal to 1, it'll enter the 'if', else it'll enter the 'else'.

    So:
    - when you have nothing in your cart, it'll add the new product
    - after that, you have 1 product, and it'll enter the 'if'

    In the 'if':
    - if the product already exists, it'll change the quantity
    - else it does nothing

    So your second product will never be inserted.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry that didn't solve the problem.

    it will update the current products quantity if the product already exits.

    the problem seems to be that as soon as there is one item in the cart it will do nothing else except update so I can't even add another new product.

    Cheers

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    But I didn't solve your problem. I only told you why it isn't working as you expected.

    I think you should change the logic in the script a bit, and make a select based on the session id AND the product code (and the update as well):
    PHP Code:
    <?php
      session_id
    ();
      
    session_start();
      include(
    "database/database.php");
      
    $qty $_POST['qty'];
      
    $prodnum mysql_real_escape_string($_POST['prodnum']);
      
    $sess session_id();

      if (empty(
    $qty) || !is_numeric($qty)) {
        include(
    "error.php");
      }

      
    $query "SELECT * from carttemp WHERE sess = '$sess' AND prodnum = '$prodnum'";
      
    $results mysql_query($query) or die(mysql_error());
      
      if (
    mysql_num_rows($results) == 1) { 
        
    $row mysql_fetch_array($results);
        
    $prodnumexists $row['prodnum'];
        
    $currentquan $row['quan'];
        
    $updateqty $currentquan $qty;
        
    mysql_query("UPDATE carttemp SET quan = '$updateqty' WHERE sess = '$sess' AND prodnum = '$prodnum'");
      }
      else {
        
    $query1 "INSERT INTO carttemp (sess, quan, prodnum) VALUES ('$sess','$qty','$prodnum')";
        
    $results1 mysql_query($query1) or die(mysql_error());
      }
      include (
    "cart.php");
      exit;
    ?>

  5. #5
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers thats spot on, thank you


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •