PDA

View Full Version : shopping cart troubles


w19sms
05-25-2008, 11:50 AM
I'm having troubles finding what my problem is. I'm taking a college course on PHP Programing with MySQL. I'm going through my textbook trying to code some of the chapter code you follow through the chapter. I've checked and double checked for typeos and have not found any.

Here is the problem when the script gets to line 91 on a second time through the loop I get this error:

Notice: Undefined index: in D:\hhs\ShoppingCart.php on line 91
Unable to perfor the query.

Error code 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE product_ID=''' at line 1

here is the code for the script:
<?php
class ShoppingCart {
private $DBConnect = "";
private $DBName = "";
private $TableName = "";
private $Orders = array();
private $OrderTable = array();

function __construct() {
$this->DBConnect = @new mysqli("localhost", "user", "password");
if (mysqli_connect_errno())
die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
}

function __destruct() {
$this->DBConnect->close();
}

public function setDatabase($Database) {
$this->DBName = $Database;
@$this->DBConnect->select_db($this->DBName)
Or die("<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($this->DBConnect)
. ": " . mysqli_error($this->DBConnect)) . "</p>";
}

public function setTable($Table) {
$this->TableName = $Table;
}

public function getProductList() {
$SQLstring = "SELECT * FROM $this->TableName";
$QueryResult = $this->DBConnect->query($SQLstring)
Or die("<p>Unable to perform the query.</p>"
. "<p>Error code " . mysqli_errno($this->DBConnect)
. ": " . mysqli_error($this->DBConnect)) . "</p>";
echo "<table width='100%' border='1' align='center' cellpadding='10'>";
echo "<tr><th>Product</th><th>Description</th><th>Price</th>";
if (isset($_SESSION['sUserName']))
echo "<th>Add Item</th>";
else
echo "</th>";
$Row = $QueryResult->fetch_row();
do {
echo "<tr><td width='14%'>{$Row[7]}</td>";
echo "<td width='56%'>{$Row[1]}{$Row[2]}{$Row[3]}{$Row[4]}{$Row[5]}</td>";
printf("<td align='center'>$%.2f ", $Row[6]);
if (isset($_SESSION['sUserName']))
echo "<td width='15%'><center> <a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=addItem&product_ID=" . $Row[0] . "'>Add</a></center> </td></tr>";
else
echo "</td></tr>";
$Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";
}

public function addItem() {
$ProdID = $_GET['product_ID'];
if (array_key_exists($ProdID, $this->Orders))
exit("<p>You already selected that item! Click your
browser's back button to return to the
previous page.</p>");
$this->Orders[$ProdID] = 1;
$this->OrderTable[$ProdID] = $this->TableName;
}

function __wakeup() {
$this->DBConnect = @new mysqli("localhost", "root", "Crystal82");
if (mysqli_connect_errno())
die("<p>Unable to connect to the database server.</p>"
. "<p>Error code ". mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
@$this->DBConnect->select_db($this->DBName)
Or die("<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($this->DBConnect)
. ": " . mysqli_error($this->DBConnect)) . "</p>";
}

public function showCart() {
if (empty($this->Orders))
echo "<p>Your shopping cart is empty!</p>";
else {
echo "<table width='100%' border='1'>";
echo "<tr><th>Remove Item</th><th>Product</th><th>Quantity</th><th>
Price Each</th></tr>";
$Total = 0;
}
foreach($this->Orders as $Order) {
/*line 91*/ $SQLstring = "SELECT * FROM " . $this->OrderTable[key($this->Orders)] . " WHERE product_ID='" . key($this->Orders) . "'";
$QueryResult = @mysqli_query($this->DBConnect, $SQLstring)
Or die("<p>Unable to perfor the query.</p>"
. "<p>Error code " . mysqli_errno($this->DBConnect)
. ": " . mysqli_error($this->DBConnect)) . "</p>";
$Row = mysqli_fetch_row($QueryResult);
echo "<td align='center'>";
echo "<a href='ShowCart.php?PHPSESSID=" . session_id()
. "&opperation=removeItem&product_ID=" . $Row[0]
. "'>Remove</a></td>";
echo "<td>{$Row[1]}</td>";
echo "<td align='center'>$Order ";
echo "<a href='ShowCart.php?PHPSESSID=" . session_id()
. "&operation=addone&product_ID=" . $Row[0] . "'>Add</a> ";
echo "<a href='ShowCart.php?PHPSESSID=" . session_id()
. "&operation=removeOne&product_ID=" . $Row[0]
. "'>Remove</a>";
echo "</td>";
printf("<td align='center'>$%.2f</td></tr>", $Row[3]);
$Total += $Row[3] * $Order;
next($this->Orders);
}
echo "<tr><td align='center'><a href='ShowCart.php?PHPSESSID="
. session_id() . "&operation=emptyCart'><strong>
Empty Cart</strong></a></td>";
echo "<td align='center' colspan='2'><strong>Yourshopping
cart contains " . count($this->Orders)
. " product(s).</strong></td>";
printf("<td align='center'><strong>Total: $%.2f</strong></td>", $Total);
echo "</table>";
}

public function removeItem() {
$ProdID = $_GET['product_ID'];
unset($this->Orders[$ProdID]);
unset($this->OrderTable[$ProdID]);
}

function emptyCart() {
$this->Orders = array();
$this->OrderTable = array();
}

public function addOne() {
$Prod = $_GET['product_ID'];
$this->Orders[$ProdID] += 1;
}

public function removeOne() {
$ProdID = $_GET['product_ID'];
$this->Orders[$ProdID] -= 1;
if ($this->Orders[$ProdID] == 0)
$this->removeItem();
}
}
?>

I did not include the other pages because they just call the functions on this page. I put a comment on line 91, and all information is passed in correctly. I have no clue why this code does not work correctly.

If anyone could help me out that would be great. Thanks in advance.

w19sms
06-02-2008, 09:52 AM
NM I figured out how to bypass this. I ended up writing my own shopping cart. So don't worry about my original question unless you want to try and figure it out. I would still like to know why it doesn't work correctly, but I will not be upset if I can't figure it out.

Thanks,
w19sms