Page 1 of 1

What's wrong with this code???

Posted: Tue Jan 23, 2007 4:53 pm
by indepthmedia

Code: Select all

foreach ($prodids as $prod) && ($qtys as qt) {
$update_sql = "UPDATE _cart SET qty=$qt WHERE prodid=$prod AND cartid=$cartid";
   $ok = @mysql_query($update_sql);
  if (!$ok) {
      echo "<p> Error updating quantities:" .
           mysql_error() . '</p>';
       }
	}
Whenever I try to put this code onto my shopping cart page (full page below) I get a blank page... and for the life of me I can't figure out why.

Code: Select all

<?php
//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>
<?php if (isset($_POST['btnUpdate']) && $_POST['btnUpdate'] == 'update') {

$prodids = $_POST['prodids'];
$qtys = $_POST[qtys];
$shipping = $_POST['shipping'];
$ship_price = $_POST[ship_price];

foreach ($prodids as $prod) && ($qtys as qt) {
$update_sql = "UPDATE _cart SET qty=$qt WHERE prodid='$prod' AND cartid='$cartid'";
   $ok = @mysql_query($update_sql);
  if (!$ok) {
      echo "<p> Error updating quantities:" .
           mysql_error() . '</p>';
       }
	}
}
 ?>
<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">array &nbsp;<?php
print_r($prodids);
echo "<br>";
print_r($qtys);
?> </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>

Posted: Tue Jan 23, 2007 4:58 pm
by matthijs
.. it's not enclosed in the proper PHP code tags so we can read it :wink:

Posted: Tue Jan 23, 2007 5:34 pm
by Kieran Huggins
it's..... GREEN!

Posted: Tue Jan 23, 2007 5:39 pm
by indepthmedia
sorry

Posted: Tue Jan 23, 2007 5:48 pm
by Kieran Huggins
try putting a die('it got this far'); statement in every so often and see how far it gets

Posted: Tue Jan 23, 2007 6:46 pm
by indepthmedia
Still nothing

Posted: Tue Jan 23, 2007 8:20 pm
by indepthmedia
Even if I create this simple test page, a blank page is returned:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

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


$prodids = array(00008, 00008, 00008, 00008);
$qtys = array(3, 1, 1, 1);

foreach ($prodids as $prod) && ($qtys as $qt) {
$update_sql = "UPDATE _cart SET qty=$qt WHERE prodid='$prod' AND cartid='$cartid'";
   $ok = @mysql_query($update_sql);
  if (!$ok) {
      echo "<p> Error updating quantities:" .
           mysql_error() . '</p>';
       }
	}

?>
</body>
</html>

Posted: Tue Jan 23, 2007 8:28 pm
by Kieran Huggins
If I'm not mistaken, this is as illegal as smuggling midgets:

Code: Select all

foreach ($prodids as $prod) && ($qtys as $qt) { }
check out http://www.php.net/manual/en/control-st ... oreach.php for the correct syntax

Posted: Tue Jan 23, 2007 9:58 pm
by feyd
The $prodids array will be filled with zeros. The reason why: a leading 0 is octal notation; 8 is outside the octal range.

Code: Select all

[feyd@home]>php -r "echo 000008;"
0

Posted: Tue Jan 23, 2007 10:47 pm
by Kieran Huggins
great catch feyd!