Page 1 of 1

My cart doesn't update the correct product amount

Posted: Mon Nov 02, 2009 11:23 am
by cstjuste
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

Re: My cart doesn't update the correct product amount

Posted: Mon Nov 02, 2009 7:53 pm
by McInfo
The first error indicates that the $id that is used to construct the query is empty. This could be because using explode() on an empty string ($cart) will still result in an array ($items) with one element. The $contents array will then contain a single element whose index ($item, $id) is an empty string.

Please use tags when posting code and use [quote][/quote] tags for errors.

Edit: This post was recovered from search engine cache.

Re: My cart doesn't update the correct product amount

Posted: Tue Nov 03, 2009 10:22 am
by cstjuste
I'm sorry about about that. I'ma check it out and recode it . Thank you and I'm going to post my results