Page 1 of 1

[56K WARN] Shopping cart update problems

Posted: Sat Jan 27, 2007 4:40 pm
by indepthmedia
Neither of my sql statements are working:

Code: Select all

//Remove item from cart
        $del_sql = "DELETE FROM _cart WHERE cartid={$cartid[$i]}";
    } else {
        //Update item quantity
        $upd_sql = "UPDATE _cart SET qty=$newqty WHERE cartid={$cartid[$i]}";
This is a snapshot of my shopping cart:
Image

And this is a snapshot after I click the "update" button:
Image

The stuff next to the update button is just so I can verify the values of my variables.

My full code is:

Code: Select all

<?php
include 'D:/inetpub/pdwsoftware/includes/proddb.inc.php';

 if (isset($_POST['btnUpdate']) && $_POST['btnUpdate'] == 'update') {

$cartids = $_POST['carts'];
$prodids = $_POST['prodids'];
$qtys = $_POST['qtys'];
$numitems = count($qtys);
$shipping = $_POST['shipping'];
$ship_price = $_POST['ship_price'];

for ($i = 0; $i < $numitems; $i++) {
	$newqty = (int)$qtys[$i];
	if ($newqty < 1) {
		//Remove item from cart
		$del_sql = "DELETE FROM _cart WHERE cartid={$cartids[$i]}";
	} else {
		//Update item quantity
		$upd_sql = "UPDATE _cart SET qty=$newqty, shipping=$shipping, ship_price=$ship_price WHERE cartid={$cartids[$i]}";
    }
}
} else {

//If the cartid cookie is NOT set yet then select the last known cartid and set a variable equal to it + 1
if (!isset($_COOKIE['PDWcart'])) {

 $cookie_sqls =  @mysql_query("SELECT cartid FROM _cart ORDER BY cartid DESC LIMIT 1");
 	while ($cookie_sql = mysql_fetch_array($cookie_sqls)) {
	$cartid = ($cookie_sql[cartid] + 1);
}
	} else { //Otherwise set $cartid equal to the current cartid cookie

 $cartid = $_COOKIE['PDWcart'];

	}
			 //Then set the cart cookie (cookie will reset if it already exists.)
 
setcookie("PDWcart", $cartid, time() + 3600 * 24 * 7);
}
?>
<html>
<head>
<title>PDW - Shopping Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="/css/basic.css" rel="stylesheet" type="text/css" media="all" />
<style type="text/css">
<!--
.style6 {color: #FF0000}
-->
</style>
</head>

<body>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="88" valign="top"><?php include 'D:/inetpub/pdwsoftware/includes/header.php'; ?></td>
  </tr>
  <tr>
    <td valign="top"><table width="703" border="0" cellspacing="0" cellpadding="0" style="margin-left:23px ">
      <tr valign="top">
        <td width="163"><?php include 'D:/inetpub/pdwsoftware/includes/left_nav.php'; ?></td>
          <td width="540"><div align="center"><br>
             
                    
              <FONT face=helvetica,arial size=3 font><strong>Your Shopping Cart  </strong><br>
            </FONT>
              <HR>
              <table width="100%" border="0">
                <tr>
                  <td><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="frmCart" id="frmCart">
                    <table width="780" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
                      <tr class="entryTableHeader">
                        <td align="center" bgcolor="#CCCCCC">Item</td>
                        <td align="center" bgcolor="#CCCCCC">Unit Price</td>
                        <td width="75" align="center" bgcolor="#CCCCCC">Quantity</td>
                        <td align="center" bgcolor="#CCCCCC">Total</td>
                        <td width="75" align="center" bgcolor="#CCCCCC">&nbsp;</td>
                      </tr>
                      <?php
include 'D:/inetpub/pdwsoftware/includes/proddb.inc.php';

$carts = @mysql_query("SELECT prodid, prodname, price, qty, shipping, ship_price FROM _cart WHERE cartid='$cartid'");
if (!$carts) {
	exit('<p>Error retrieving carts from database!<br />'.
		'Error: ' . mysql_error() . '</p>');
}

while ($cart = mysql_fetch_array($carts)) {
	$prodid = $cart['prodid'];
	$qty = $cart[qty];
	$price = $cart[price];
	$shipping = $cart['shipping'];
	$ship_price = $cart[ship_price];
	$linetotal = number_format(($price * $qty), 2);
	$prodname = htmlspecialchars($cart['prodname']);
	$lines[] = $cart;
	$subtotal += $price * $qty;
	echo "<tr class='content'>".
                        "<td align='center'>$prodname</td>".
                        "<td align='right'>$$price</td>".
                        "<td width='75'><input name='qtys[]' type='text' id='qtys[]' size='5' value='$qty' class='box' onKeyUp='checkNumber(this);'>".
                            "<input name='carts[]' type='hidden' value='$cartid'>".
                            "<input name='prodids[]' type='hidden' value='$prodid'></td>".
                        "<td align='right'>$$linetotal</td>".
                        "<td width='75' align='center'><input name='btnDelete' type='button' id='btnDelete' value='Delete' class='box'>".                        "</td>".
                      "</tr>";
}
$numItem = count($lines);
?>
                      <tr class="content">
                        <td colspan="3" align="right">Sub-total</td>
                        <td align="right">$<?php echo number_format($subtotal, 2); ?></td>
                        <td width="75" align="center">&nbsp;</td>
                      </tr>
                      <tr class="content">
                        <td colspan="3" align="right">Shipping </td>
                        <td align="right">
                            <select name="shipping">
							  <option value="<?php echo $ship_price; ?>"><?php echo $shipping; ?></option>
<?php if ($shipping != 'Fedex Ground - $25.00') { echo "<option value='25.00'>Fedex Ground - $25.00</option>"; } ?>
<?php if ($shipping != 'Fedex 2-Day - $120.00') { echo "<option value='120.00'>Fedex 2-Day - $120.00</option>"; } ?>
<?php if ($shipping != 'Fedex Overnight - $149.00') { echo "<option value='149.00'>Fedex Overnight - $149.00</option>"; } ?>
                            </select>
                        </td>
                        <td width="75" align="center">&nbsp;</td>
                      </tr>
                      <tr class="content">
                        <td colspan="3" align="right">Total </td>
                        <td align="right"></td>
                        <td width="75" align="center">&nbsp;</td>
                      </tr>
                      <tr class="content">
                        <td colspan="4" align="right">New Qty:<?php echo $newqty; ?> Num Items: <?php echo $numitems; ?>
                          <input type="hidden" name="<?php echo $cartid; ?>">
                       prodids &nbsp;<?php
print_r($prodids);
echo "<br> qtys ";
print_r($qtys);
echo "<br> cartids ";
print_r($cartids);
?> </td>
                        <td width="75" align="center"><input name="btnUpdate" type="submit" id="btnUpdate" value="update" class="box"></td>
                      </tr>
                    </table>
                  </form></td>
                </tr>
              </table>
              <br>
            </div>          </td>
      </tr>
    </table>
    <?php include 'D:/inetpub/pdwsoftware/includes/footer.php'; ?></td>
  </tr>
</body>
</html>
I'm not having any trouble displaying the cart contents the first time around but my code for updating the cart is where all my trouble is:

Code: Select all

include 'D:/inetpub/pdwsoftware/includes/proddb.inc.php';

 if (isset($_POST['btnUpdate']) && $_POST['btnUpdate'] == 'update') {

$cartids = $_POST['carts'];
$prodids = $_POST['prodids'];
$qtys = $_POST['qtys'];
$numitems = count($qtys);
$shipping = $_POST['shipping'];
$ship_price = $_POST['ship_price'];

for ($i = 0; $i < $numitems; $i++) {
	$newqty = (int)$qtys[$i];
	if ($newqty < 1) {
		//Remove item from cart
		$del_sql = "DELETE FROM _cart WHERE cartid={$cartids[$i]}";
	} else {
		//Update item quantity
		$upd_sql = "UPDATE _cart SET qty=$newqty, shipping=$shipping, ship_price=$ship_price WHERE cartid={$cartids[$i]}";
    }
}
} else {

Posted: Sat Jan 27, 2007 5:48 pm
by louie35
did you try writting the code as it goes see if it goes inside the if statement?
like this:

Code: Select all

if (isset($_POST['btnUpdate']) && $_POST['btnUpdate'] == 'update') { 

$cartids = $_POST['carts']; 
$prodids = $_POST['prodids']; 
$qtys = $_POST['qtys']; 
$numitems = count($qtys); 
$shipping = $_POST['shipping']; 
$ship_price = $_POST['ship_price']; 

for ($i = 0; $i < $numitems; $i++) { 
        $newqty = (int)$qtys[$i]; 
        if ($newqty < 1) { 
                //Remove item from cart 
                $del_sql = "DELETE FROM _cart WHERE cartid={$cartids[$i]}"; 
                echo $del_sql ;
                die();
        } else { 
                //Update item quantity 
                $upd_sql = "UPDATE _cart SET qty=$newqty, shipping=$shipping, ship_price=$ship_price WHERE cartid={$cartids[$i]}"; 
                echo $upd_sql ;
                die();

    } 
} 
} else {
also try changing this

Code: Select all

if (isset($_POST['btnUpdate']) && $_POST['btnUpdate'] == 'update') { 
//to this
 if (isset($_POST['btnUpdate'])) {

what kind of Database are you working with?

Posted: Sat Jan 27, 2007 6:10 pm
by nmpdesign
hey, Neil here. what DB are you using? MySQL, Microsoft SQL Server 2005? 2000?

Posted: Sun Jan 28, 2007 3:30 pm
by indepthmedia
Thanks for helping guys.

Louie35, when I add in your die suggestions I get "UPDATE _cart SET qty=1, shipping=25.00, ship_price= WHERE cartid=10" returned to me after clicking the update button.

Posted: Sun Jan 28, 2007 3:50 pm
by louie35

Code: Select all

"UPDATE _cart SET qty=1, shipping=25.00, ship_price= WHERE cartid=10"
the ship_price seems that it has no value there, and if you don't allow null value in the database it will trow you an error.

Posted: Sun Jan 28, 2007 4:11 pm
by indepthmedia
Ok, so I removed the shipping and ship_price columns from being updated just so I can debug this easier. Also, someone else helped me realize I forgot to actually call my sql variables:

So I changed:

Code: Select all

//Remove item from cart
                $del_sql = "DELETE FROM _cart WHERE cartid={$cartids[$i]}";
                echo $del_sql ;
                die();
        } else {
                //Update item quantity
                $upd_sql = "UPDATE _cart SET qty=$newqty WHERE cartid={$cartids[$i]}";
                echo $upd_sql ;
                die();
To:

Code: Select all

//Remove item from cart
                $del_sql = @mysql_query("DELETE FROM _cart WHERE cartid={$cartids[$i]}");
                echo $del_sql ;
                die();
        } else {
                //Update item quantity
                $upd_sql = @mysql_query("UPDATE _cart SET qty=$newqty WHERE cartid={$cartids[$i]}");
                echo $upd_sql ;
                die();
So now the database is updating but it is updating the quantity for each item to whatever I set the first items quantity to:
Image

Becomes:
Image

Posted: Sun Jan 28, 2007 5:18 pm
by louie35
that becuse you are updating the cart id, and if you have 3 products for this session in the cart then all 3 has the same cart id. you need to pass another variable to the cart update / delete like product id or probably product name so it knows which line to act on.

I don't know how your database is setup but you must have some sort of unique id there as well that increments by one when a new row is added.

Posted: Sun Jan 28, 2007 10:05 pm
by indepthmedia
Thank you so much!

I finally have this working now. :D