OK, good tip,
Messing with the syntax of the query I now have this printing to the browser:
partnum= CB09B
datatable= GiftGallery
SELECT * FROM GiftGallery WHERE PARTNUM LIKE CB09B IN (CAV67,CAV68,C0AAA,C4C46,CB09😎 ORDER BY partnum ASC
I added 5 parts to the shopping cart thats what all the CXXXX numbers are.
Anyhow, now I get the same error with the while loop. I changed the code dramatically, I know it is bad coding, but I prefer weaving PHP into html. In dreamweaver it makes things a little more visual instead of using echo statements.
Here is the updated code:
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td colspan="2"><p class="style9">My Shopping Cart
<p class="style9">
<?php
$partnum = $_GET['PARTNUM'];
$datatable = $_GET['DB'];
print "partnum= $partnum<br>";
print "datatable= $datatable<br><br>";
if (isset($partnum)) {
if (isset ($_SESSION['cart'][$partnum])) {
$qty = $_SESSION['cart'][$partnum] + 1;
}
else {
$qty = 1;
}
$_SESSION['cart'][$partnum] = $qty;
}
else {
header ("Location: http//" .$_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
exit();
}
?>
<?php
#Script 13.9 - view_cart.php
if(isset ($_POST['submit'])){
foreach ($_POST['qty'] as $key => $value) {
if(($value == 0) AND (is_numeric($value))){
unset ($_SESSION['cart'][$key]);
}
elseif (is_numeric ($value) AND ($value > 0) ){
$_SESSION['cart'][$key] = $value;
}
}
}
//check if the shopping cart is empty.
$empty = TRUE;
if (isset ($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)){
$empty = FALSE;
}
}
}
//Display the cart if it is not empty.
if(!$empty){
// INITIALIZING VARIABLES
$hostname = "localhost";
$username = "xxxxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";
$usertable = $datatable;
mysql_connect($hostname,$username,$password) or die("Unable to connect to database");
mysql_select_db($dbname) or die("Unable to select database");
$query = "SELECT * FROM $usertable WHERE PARTNUM LIKE $partnum IN (";
foreach ($_SESSION['cart'] as $key => $value){
$query .= $key . ",";
}
$query = substr ($query, 0, -1) . ") ORDER BY partnum ASC";
$result = mysql_query ($query);
print $query;
?>
<form name="form1" method="post" action="">
<p class="style9">
<table border="1" width="90%" cellspacing="0" cellpadding="3" align="center" bordercolor="#E0DFE3">
<tr bgcolor="#4E7B98" class="style2">
<td width="12%"><div align="center" class="style15">Part Number </div></td>
<td width="48%"><div align="center" class="style15">Description</div></td>
<td width="10%"><div align="center"><span class="style15">Price</span></div></td>
<td width="13%"><div align="center"><span class="style15">Quantity</span></div></td>
<td width="17%"><div align="center" class="style15">Line Total</div></td>
</tr>
<?php
//Print each item.
$total = 0; //total cost of each order
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
//Calculate the totals and subtotals.
$subtotal = $_SESSION['cart'][$row['partnum']] * $row['price'];
$total += $subtotal;
//Print the row
?>
<tr>
<td align=left><?php print "{$row['partnum']}"; ?></td>
<td align=left><?php print "{$row['description']}"; ?></td>
<td align=right>$<?php print "{$row['price']}"; ?> </td>
<td align=center><input type=text size=3 name=<?php print "qty[{$row['partnum']}]"; ?>
value=<?php print "{$_SESSION['cart'][$row['partnum']]}"; ?>></td>
<td align=right>$<?php print ' . number_format (subtotal, 2) . '; ?></td>
</tr>
<?php
} //end of while loop
?>
<tr>
<td colspan="4" align="right"><b>Total:<b></b></b></td>
<td align="right">$
<?php $total = number_format ($total, 2); print $total; ?></td>
</tr>
</table>
<div align="center">
<input type="submit" name="submit" value="Update My Cart"/>
<br>
<br>
<a href="checkout.php"><font size="+3">Checkout</font></a></div>
<?php
//Close the database connection
mysql_close();
}
else{
echo '<p>Your cart is empty.</p>';
}
?>
</form> <p class="style9">
</td>
</tr>
</table>
The error I get is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home3/tchbyrd/tool-link-www/shoppingcart.php on line 281
Line 281 is:
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
In all the data checking I do, I cant seem to track the values of {$row['partnum']}, any of the row values for that matter. I think there is a strong chance, the query is not putting any values into the $row array. I think I am having trouble conceptualizing stuff like
$_SESSION['cart'][$row['partnum']]
This is supposed to be a quantity to be used in row values. Arrays are mind boggling.
Again I thank you in advance.
Shanis