My cart doesn't update the correct product amount
Posted: Mon Nov 02, 2009 11:23 am
Hi Everyone,
I've been working on this code for my shopping cart. The problem is that when I try to update the cart with additional items, it adds the items but also gives me a couple of error messages. For instance, if I have 1 of product A in my cart, and I want to have 3 instead of 1, it gives me a total of 4.
Product A - 1
Product A - 3
When I try to remove the the excess product, it removes both line items out of my cart. I was trying to figure this out all day yesterday, but any help would be much appreciated.
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM apple WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row, EXTR_PREFIX_SAME, 'ink');
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$lip.' '.$printerT.'</td>';
$output[] = '<td><input type="text" name="qty" value="'.$qty.'" size="3" maxlength="3" /></td>';
$total += $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Total Quantity: <strong>'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
and here is the function that calls the query
/**
* Returns an instance of MySQLResult to fetch rows with
* @param $sql string the database query to run
* @return MySQLResult
* @access public
*/
function query($sql) {
if (!$queryResource=mysql_query($sql,$this->dbConn))
trigger_error ('Query failed: '.mysql_error($this->dbConn).
' SQL: '.$sql);
return new MySQLResult($this,$queryResource);
}
}
and here is the fetch function
/**
* Fetches a row from the result
* @return array
* @access public
*/
function fetch () {
if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) {
return $row;
} else if ( $this->size() > 0 ) {
mysql_data_seek($this->query,0);
return false;
} else {
return false;
}
}
Notice: Query failed: 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 '' at line 1 SQL: SELECT * FROM apple WHERE id = in /home/waldon/public_html/inc/mysql.class.php on line 109
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/waldon/public_html/inc/mysql.class.php on line 151
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/waldon/public_html/inc/mysql.class.php on line 167
Warning: extract() [function.extract]: First argument should be an array in /home/waldon/public_html/inc/functions.inc.php on line 29
I've been working on this code for my shopping cart. The problem is that when I try to update the cart with additional items, it adds the items but also gives me a couple of error messages. For instance, if I have 1 of product A in my cart, and I want to have 3 instead of 1, it gives me a total of 4.
Product A - 1
Product A - 3
When I try to remove the the excess product, it removes both line items out of my cart. I was trying to figure this out all day yesterday, but any help would be much appreciated.
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM apple WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row, EXTR_PREFIX_SAME, 'ink');
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$lip.' '.$printerT.'</td>';
$output[] = '<td><input type="text" name="qty" value="'.$qty.'" size="3" maxlength="3" /></td>';
$total += $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Total Quantity: <strong>'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
and here is the function that calls the query
/**
* Returns an instance of MySQLResult to fetch rows with
* @param $sql string the database query to run
* @return MySQLResult
* @access public
*/
function query($sql) {
if (!$queryResource=mysql_query($sql,$this->dbConn))
trigger_error ('Query failed: '.mysql_error($this->dbConn).
' SQL: '.$sql);
return new MySQLResult($this,$queryResource);
}
}
and here is the fetch function
/**
* Fetches a row from the result
* @return array
* @access public
*/
function fetch () {
if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) {
return $row;
} else if ( $this->size() > 0 ) {
mysql_data_seek($this->query,0);
return false;
} else {
return false;
}
}
Notice: Query failed: 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 '' at line 1 SQL: SELECT * FROM apple WHERE id = in /home/waldon/public_html/inc/mysql.class.php on line 109
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/waldon/public_html/inc/mysql.class.php on line 151
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/waldon/public_html/inc/mysql.class.php on line 167
Warning: extract() [function.extract]: First argument should be an array in /home/waldon/public_html/inc/functions.inc.php on line 29