Mark Steggles
asked on
Fatal error: Call to a member function query() on a non-object in
Hey guys,
I'm trying to learn php and making my first cart at the moment. Found a tutorial online and have been able to save the cart contents into a session but I'm stuck on how to connect to the database to match the cart with the products in the database table.
this line
$result = $con->query($sql);
is causing the error...
Any help appreciated
I'm trying to learn php and making my first cart at the moment. Found a tutorial online and have been able to save the cart contents into a session but I'm stuck on how to connect to the database to match the cart with the products in the database table.
this line
$result = $con->query($sql);
is causing the error...
Any help appreciated
<?php
// login details for database
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
// connect to database
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
// select products database
//mysql_select_db("futurek0_products", $con);
$total = 0;
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE ID = '.$id;
$result = $con->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($PRODUCT_PRICE * $qty).'</td>';
$total += $PRODUCT_PRICE * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: £'.$total.'</p>';
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try write
$sql = 'SELECT * FROM products WHERE ID = '.$id; //why dot? it misses a '
$sql = 'SELECT * FROM products WHERE ID = '$id';
Then use this
while ($row = mysql_fetch_array($result) {
echo '<tr>';
echo '<td>' . $row['PRODUCT_DESC'] . '</td>';
echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
echo '<td>$' . $row['PRICE'] . '</td>';
echo '<td>$' . ($row["PRICE"] * $qty) . '</td>';
echo '<td><a href="cart.php?action=dele te&id='.$i d.'" class="r">Remove</a></td>' ;
$total += $row['PRICE'] * $qty;
echo '</tr>';
}
I stronglky recommend you to read this tutorial and related book: http://articles.sitepoint.com/article/php-mysql-tutorial.
Good luck
$sql = 'SELECT * FROM products WHERE ID = '.$id; //why dot? it misses a '
$sql = 'SELECT * FROM products WHERE ID = '$id';
Then use this
while ($row = mysql_fetch_array($result)
echo '<tr>';
echo '<td>' . $row['PRODUCT_DESC'] . '</td>';
echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
echo '<td>$' . $row['PRICE'] . '</td>';
echo '<td>$' . ($row["PRICE"] * $qty) . '</td>';
echo '<td><a href="cart.php?action=dele
$total += $row['PRICE'] * $qty;
echo '</tr>';
}
I stronglky recommend you to read this tutorial and related book: http://articles.sitepoint.com/article/php-mysql-tutorial.
Good luck
ASKER
thanks guys
ASKER
I reworked the code (see attached) from your advice and am now getting results. The tutorial I was using is http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart
--
Thanks Ray, that code looks helpful.
--
One problem is that I'm only getting one result back when I loop through the cart. Something wrong with
foreach ($contents as $id=>$qty) { ?
Thanks
Open in new window