[56K WARN] Shopping cart update problems
Posted: Sat Jan 27, 2007 4:40 pm
Neither of my sql statements are working:
This is a snapshot of my shopping cart:

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

The stuff next to the update button is just so I can verify the values of my variables.
My full code is:
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
//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]}";
And this is a snapshot after I click the "update" button:

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"> </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"> </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"> </td>
</tr>
<tr class="content">
<td colspan="3" align="right">Total </td>
<td align="right"></td>
<td width="75" align="center"> </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 <?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>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 {
