showcart.php in Shopping Cart

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 32231 Views - Last Post: 28 March 2011 - 08:24 AM Rate Topic: -----

#1 didi2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

showcart.php in Shopping Cart

Posted 15 February 2011 - 07:23 AM

Practical PHP and MySQL@Building Eight Dynamic Web Applications by Jono Bacon

Referring to above book, i've tried to create a shopping cart according to the sample codes provided by above books.
But when dealing with showcart.php where it require a showcart() function, there is an problem. Whenever i included the showcart() in showcart.php, showcart.php is totally not function. I've no idea what's wrong with the coding...
I've tried to separate the showcart() function to another showcartfunction.php or directly added the codes inside the showcart.php, but both also giving same result.


showcartfunction.php
<?php
function showcart()
{
	if($_SESSION['SESS_ORDERNUM'])
		{
			if($_SESSION['SESS_LOGGEDIN'])
				{
					$custsql = "SELECT id, status from
					orders WHERE customer_id = "
					. $_SESSION['SESS_USERID']
					. " AND status < 2;";
					$custres = mysql_query($custsql);
					$custrow = mysql_fetch_assoc($custres);
					$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS
					itemid FROM products, orderitems WHERE orderitems.product_id =
					products.id AND order_id = " . $custrow['id'] .;
					$itemsres = mysql_query($itemssql);
					$itemnumrows = mysql_num_rows($itemsres);
				}
		else
				{
					$custsql = "SELECT id, status from orders
					WHERE session = '" . session_id() . "' AND status < 2;";
					$custres = mysql_query($custsql);
					$custrow = mysql_fetch_assoc($custres);
					$itemssql = "SELECT products.*,
					orderitems.*, orderitems.id AS itemid
					FROM products, orderitems WHERE
					orderitems.product_id = products.id AND
					order_id = " . $custrow['id'] .;
					$itemsres = mysql_query($itemssql);
					$itemnumrows = mysql_num_rows($itemsres);
				}
		}
	else
		{
			$itemnumrows = 0;
		}
			if($itemnumrows == 0)
				{
					echo "You have not added anything to your shopping cart yet.";
				}
			else
				{
					echo "<table cellpadding='10'>";
					echo "<tr>";
					echo "<td></td>";
					echo "<td><strong>Item</strong></td>";
					echo "<td><strong>Quantity</strong></td>";
					echo "<td><strong>Unit Price</strong></td>";
					echo "<td><strong>Total Price</strong></td>";
					echo "<td></td>";
					echo "</tr>";

while($itemsrow = mysql_fetch_assoc($itemsres))
	{
$quantitytotal =
$itemsrow['price'] * $itemsrow['quantity'];
echo "<tr>";
if(empty($itemsrow['image'])) {
echo "<td><img
src='./productimages/dummy.jpg' width='50' alt='"
. $itemsrow['name'] . "'></td>";
}
else {
echo "<td><img src='./productimages/" .
$itemsrow['image'] . "' width='50' alt='"
. $itemsrow['name'] . "'></td>";
}
echo "<td>" . $itemsrow['name'] . "</td>";
echo "<td>" . $itemsrow['quantity'] . "</td>";
echo "<td><strong>&pound;"
sprintf('%.2f', $itemsrow['price'])
"</strong></td>";
echo "<td><strong>&pound;"
sprintf('%.2f', $quantitytotal) . "</strong></td>";
echo "<td>[<a href='"
$config_basedir . "delete.php?id="
$itemsrow['itemid'] . "'>X</a>]</td>";
echo "</tr>";
$total = $total + $quantitytotal;
$totalsql = "UPDATE orders SET total = "
. $total . " WHERE id = ". $_SESSION['SESS_ORDERNUM'] .;
$totalres = mysql_query($totalsql);
}
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>TOTAL</td>";
echo "<td><strong>RM"
. sprintf('%.2f', $total) . "</strong></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
}
}

?>



showcart.php
<?php
session_start();
require("header.php");
require("showcartfunction.php");
echo "<h1>Your shopping cart</h1>";

showcart();

if(isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
$sql = "SELECT * FROM orderitems WHERE
order_id = " . $_SESSION['SESS_ORDERNUM'] . ";";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
if($numrows >= 1) {
echo "<h2><a href='checkout-address.php'>
Go to the checkout</a></h2>";
}
}
require("footer.php");
?>



Is This A Good Question/Topic? 0
  • +

Replies To: showcart.php in Shopping Cart

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,796
  • Joined: 27-December 08

Re: showcart.php in Shopping Cart

Posted 15 February 2011 - 08:33 AM

You don't define the showcart() function. That seems to be your problem.Also, in the future, please post your specific errors if you receive any.

In addition, not good practice to mix PHP and HTML. Keep them separate. PHP should treat the HTML files as templates, reading them in using file_get_contents() and modifying them, or using an include() statement to include the HTML.

This post has been edited by macosxnerd101: 19 February 2011 - 11:44 PM
Reason for edit:: Scratch that. I missed the function definition.

Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: showcart.php in Shopping Cart

Posted 15 February 2011 - 10:19 AM

@macosxnerd101: He does define showcart(). It's in showcartfunction.php, and is required.

@didi2005: If there is literally zero output at all, have you considered that perhaps it's throwing an error? You should check the directory the script is running from for an error log file from PHP. You could also check to ensure the ini directive 'display_errors' is on via ini_get(). If it's not, make sure your 'error_log' directive is set to a path you can get to, so you can read any errors PHP might be throwing from your page.

Additionally, while testing, you'll likely want to crank up the level of errors it's reporting with error_reporting(E_ALL); just so you can see all of the stuff wrong with your scripts while working on them.

This post has been edited by Valek: 15 February 2011 - 10:27 AM

Was This Post Helpful? 1
  • +
  • -

#4 didi2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Re: showcart.php in Shopping Cart

Posted 17 February 2011 - 04:01 AM

Below is my latest showcart.php

<?php
session_start();
require("header.php");
echo "<h1>Your shopping cart</h1>";

//Start
if($_SESSION['SESS_ORDERNUM'])
		{
			if($_SESSION['SESS_LOGGEDIN'])
				{
					$custsql = "SELECT id, status from
					orders WHERE customer_id = "
					. $_SESSION['SESS_USERID']
					. " AND status < 2;";
					$custres = mysql_query($custsql);
					$custrow = mysql_fetch_assoc($custres);
					$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS
					itemid FROM products, orderitems WHERE orderitems.product_id =
					products.id AND order_id = ". $custrow['id'];
					$itemsres = mysql_query($itemssql);
					$itemnumrows = mysql_num_rows($itemsres);
				}
		else
				{
					$custsql = "SELECT id, status from orders
					WHERE session = '" . session_id() . "' AND status < 2;";
					$custres = mysql_query($custsql);
					$custrow = mysql_fetch_assoc($custres);
					$itemssql = "SELECT products.*,
					orderitems.*, orderitems.id AS itemid
					FROM products, orderitems WHERE
					orderitems.product_id = products.id AND
					order_id = " . $custrow['id'];
					$itemsres = mysql_query($itemssql);
					$itemnumrows = mysql_num_rows($itemsres);
				}
		}
	else
		{
			$itemnumrows = 0;
		}
			if($itemnumrows == 0)
				{
					echo "You have not added anything to your shopping cart yet.";
				}
			else
				{
					echo "<table cellpadding='10'>";
					echo "<tr>";
					echo "<td></td>";
					echo "<td><strong>Item</strong></td>";
					echo "<td><strong>Quantity</strong></td>";
					echo "<td><strong>Unit Price</strong></td>";
					echo "<td><strong>Total Price</strong></td>";
					echo "<td></td>";
					echo "</tr>";

while($itemsrow = mysql_fetch_assoc($itemsres))
	{
$quantitytotal =
$itemsrow['price'] * $itemsrow['quantity'];
echo "<tr>";
if(empty($itemsrow['image'])) {
echo "<td><img
src='./productimages/dummy.jpg' width='50' alt='"
. $itemsrow['name'] . "'></td>";
}
else {
echo "<td><img src='./productimages/" .
$itemsrow['image'] . "' width='50' alt='"
. $itemsrow['name'] . "'></td>";
}
echo "<td>" . $itemsrow['name'] . "</td>";
echo "<td>" . $itemsrow['quantity'] . "</td>";
echo "<td><strong>RM"
. sprintf('%.2f', $itemsrow['price'])
. "</strong></td>";
echo "<td><strong>&pound;"
. sprintf('%.2f', $quantitytotal) . "</strong></td>";
echo "<td>[<a href='"
. $config_basedir . "delete.php?id="
. $itemsrow['itemid'] . "'>X</a>]</td>";
echo "</tr>";
$total = $total + $quantitytotal;
$totalsql = "UPDATE orders SET total = "
. $total . " WHERE id = ". $_SESSION['SESS_ORDERNUM'] ;
$totalres = mysql_query($totalsql);
}
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>TOTAL</td>";
echo "<td><strong>RM"
. sprintf('%.2f', $total) . "</strong></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
}
//End

if(isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
$sql = "SELECT * FROM orderitems WHERE
order_id = " . $_SESSION['SESS_ORDERNUM'] . ";";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
if($numrows >= 1) {
echo "<h2><a href='checkout-address.php'>
Go to the checkout</a></h2>";
}
}
require("footer.php");
?>



Whenever i pressed the add to basket button, below message will shown on my showcart.php page.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/diyersitzo/domains/dslr-canon.co.cc/public_html/sites/shoppingcart/showcart.php on line 35
You have not added anything to your shopping cart yet.
Go to the checkout

Attached also the addtobasket.php file.
<?php
session_start();
require("db.php");
require("functions.php");
$validid = pf_validate_number($_GET['id'],
"redirect", $config_basedir);
$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$prodres = mysql_query($prodsql);
$numrows = mysql_num_rows($prodres);
$prodrow = mysql_fetch_assoc($prodres);
if($numrows == 0)
{
header("Location: " . $config_basedir);
}
else
{
if($_POST['submit'])
{
if($_SESSION['SESS_ORDERNUM'])
{
$itemsql = "INSERT INTO orderitems(order_id,
product_id, quantity) VALUES("
. $_SESSION['SESS_ORDERNUM'] . ", "
. $_GET['id'] . ", "
. $_POST['amountBox'] . ")";
mysql_query($itemsql);
}
else
{
if($_SESSION['SESS_LOGGEDIN'])
{
$sql = "INSERT INTO orders(customer_id,
registered, date) VALUES("
. $_SESSION['SESS_USERID'] . ", 1, NOW())";
mysql_query($sql);
session_register("SESS_ORDERNUM");
$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
$itemsql = "INSERT INTO
orderitems(order_id, product_id, quantity) VALUES("
. $_SESSION['SESS_ORDERNUM']
. ", " . $_GET['id'] . ", "
. $_POST['amountBox'] . ")";
mysql_query($itemsql);
}
else
{
$sql = "INSERT INTO orders(registered,
date, session) VALUES("
. "0, NOW(), '" . session_id() . "')";
mysql_query($sql);
session_register("SESS_ORDERNUM");
$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
$itemsql = "INSERT INTO
orderitems(order_id, product_id, quantity) VALUES("
. $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", "
. $_POST['amountBox'] . ")";
mysql_query($itemsql);
}
}
$totalprice = $prodrow['price'] * $_POST['amountBox'] ;
$updsql = "UPDATE orders SET total = total + "
. $totalprice . " WHERE id = "
. $_SESSION['SESS_ORDERNUM'] . ";";
mysql_query($updres);
header("Location: " . $config_basedir . "showcart.php");
}
else
{
require("header.php");
echo "<form action='addtobasket.php?id="
. $_GET['id'] . "' method='POST'>";
echo "<table cellpadding='10'>";
echo "<tr>";
if(empty($prodrow['image'])) {
echo "<td><img
src='./productimages/dummy.jpg' width='50' alt='"
. $prodrow['name'] . "'></td>";
}
else {
echo "<td>
<img src='./productimages/" . $prodrow['image']
. "' width='50' alt='" . $prodrow['name']
. "'></td>";
}
echo "<td>" . $prodrow['name'] . "</td>";
echo "<td>Select Quantity <select name='amountBox'>";
for($i=1;$i<=5;$i++)
{
echo "<option>" . $i . "</option>";
}
echo "</select></td>";
echo "<td><strong>RM"
. sprintf('%.2f', $prodrow['price'])
. "</strong></td>";
echo "<td><input type='submit'
name='submit' value='Add to basket'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
}
require("footer.php");
?>



The shopping cart can be accessible via http://dslr-canon.co...s/shoppingcart/. U guys can try add product to basket and will see the problem. I've checked through the orders & orderitems tables, bot got row of data when i add product to basket...

Posted Image

This post has been edited by didi2005: 17 February 2011 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: showcart.php in Shopping Cart

Posted 17 February 2011 - 05:03 AM

See problem #3 in this FAQ.
Was This Post Helpful? 1
  • +
  • -

#6 didi2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Re: showcart.php in Shopping Cart

Posted 18 February 2011 - 04:37 AM

I've inserted die mysql_error(). After add item to cart, showcart.php show "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 5".
Might be sql query error. But i wonder what is the meaning of line 5, because when checked back the showcart.php, line 5 is empty...
Before i add any item to basket, the showcart.php show correct action.
Was This Post Helpful? 0
  • +
  • -

#7 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: showcart.php in Shopping Cart

Posted 18 February 2011 - 09:58 AM

What is $validid for?
You are not using it in your SQL Queries
Was This Post Helpful? 0
  • +
  • -

#8 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: showcart.php in Shopping Cart

Posted 18 February 2011 - 10:13 AM

Do your SQL Queries work?
I am not sure if what Queries are legit, someone can correct me if I am wrong

But I dont know if you can pull from 2 tables like that.. Looks like your trying to perform a Join, so I went ahead and made your code a little easier to read and made fixes to your SQL Queries.

Can you echo out the session_id() to compare to whats in the database?

You are pulling from the Database based off of session_id() but you are NOT checking to see if there is a valid record.

If there is not a valid record, then $custrow['ID'] will be blank.

$itemssql = "SELECT products.*,
			  orderitems.*, 
			  orderitems.id AS itemid
		   FROM products
		   INNER JOIN orderitems
		   ON orderitems.product_id = products.id 
		   WHERE order_id = " . $custrow['id'];




Below is your code with the new SQL Queries

<?php
session_start();
require("header.php");
echo "<h1>Your shopping cart</h1>";

if($_SESSION['SESS_ORDERNUM'])
{
	if($_SESSION['SESS_LOGGEDIN'])
	{
		$custsql = "SELECT id, status from
		orders WHERE customer_id = "
		. $_SESSION['SESS_USERID']
		. " AND status < 2;";
		$custres = mysql_query($custsql);
		$custrow = mysql_fetch_array($custres);
		$itemssql = "SELECT products.*, 
							orderitems.*, 
							orderitems.id AS itemid 
					 FROM products
					 INNER JOIN orderitems 
					 ON orderitems.product_id = products.id 
					 WHERE order_id = ". $custrow['id'];
		$itemsres = mysql_query($itemssql);
		$itemnumrows = mysql_num_rows($itemsres);
	}
	else
	{
		$custsql = "SELECT id, status from orders WHERE session = '" . session_id() . "' AND status < 2;";
		$custres = mysql_query($custsql);
		$custrow = mysql_fetch_array($custres);
		$itemssql = "SELECT products.*,
							orderitems.*, 
							orderitems.id AS itemid
					 FROM products
					 INNER JOIN orderitems
					 ON orderitems.product_id = products.id 
					 WHERE order_id = " . $custrow['id'];
		$itemsres = mysql_query($itemssql);
		$itemnumrows = mysql_num_rows($itemsres);
	}
}
else
{
	$itemnumrows = 0;
}

if($itemnumrows == 0)
{
	echo "You have not added anything to your shopping cart yet.";
}
else
{
	echo "<table cellpadding='10'>";
	echo "<tr>";
	echo "<td></td>";
	echo "<td><strong>Item</strong></td>";
	echo "<td><strong>Quantity</strong></td>";
	echo "<td><strong>Unit Price</strong></td>";
	echo "<td><strong>Total Price</strong></td>";
	echo "<td></td>";
	echo "</tr>";

	while($itemsrow = mysql_fetch_assoc($itemsres))
	{
		$quantitytotal = $itemsrow['price'] * $itemsrow['quantity'];
		echo "<tr>";
		if(empty($itemsrow['image'])) {
			echo "<td><img src='./productimages/dummy.jpg' width='50' alt='" . $itemsrow['name'] . "'></td>";
		}
		else 
		{
			echo "<td><img src='./productimages/" .	$itemsrow['image'] . "' width='50' alt='" . $itemsrow['name'] . "'></td>";
		}
		echo "<td>" . $itemsrow['name'] . "</td>";
		echo "<td>" . $itemsrow['quantity'] . "</td>";
		echo "<td><strong>RM" . sprintf('%.2f', $itemsrow['price']) . "</strong></td>";
		echo "<td><strong>&pound;" . sprintf('%.2f', $quantitytotal) . "</strong></td>";
		echo "<td>[<a href='". $config_basedir . "delete.php?id=" . $itemsrow['itemid'] . "'>X</a>]</td>";
		echo "</tr>";
		$total = $total + $quantitytotal;
		$totalsql = "UPDATE orders SET total = ". $total . " WHERE id = ". $_SESSION['SESS_ORDERNUM'] ;
		$totalres = mysql_query($totalsql);
	}
	echo "<tr>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td>TOTAL</td>";
	echo "<td><strong>RM" . sprintf('%.2f', $total) . "</strong></td>";
	echo "<td></td>";
	echo "</tr>";
	echo "</table>";
	echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
}


if(isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
	$sql = "SELECT * FROM orderitems WHERE order_id = " . $_SESSION['SESS_ORDERNUM'] . ";";
	$result = mysql_query($sql);
	$numrows = mysql_num_rows($result);
	if($numrows >= 1) {
		echo "<h2><a href='checkout-address.php'>Go to the checkout</a></h2>";
	}
}
require("footer.php");
?>


This post has been edited by VolcomMky: 18 February 2011 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: showcart.php in Shopping Cart

Posted 18 February 2011 - 01:09 PM

It's worth noting that date is a reserved word in MySQL and should be surrounded in backquotes (`) just to guarantee MySQL doesn't get confused as to your intentions. That said, I don't believe that is the cause of your problems. Instead of just doing die(mysql_error());, check the query's success directly with an if statement. If it fails, dump the query to a file so you can see how it's being built, and then have it die with mysql_error(). Then you can see what error you're getting, and looking at your query, you'll find out why.
Was This Post Helpful? 0
  • +
  • -

#10 didi2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Re: showcart.php in Shopping Cart

Posted 07 March 2011 - 07:58 AM

Suspect problem came form addtobasket.php ~~~

Before i add any items, i click the ViewBasket/Checkout link(showcart.php), it show correct result.
Posted Image

Then i went to add an item to basket.
Posted Image

Instead of directing to showcart.php page that shows the content of the cart, it hanged on addtobasket.php showing empty query...
Posted Image

I've checked the databases for table orders & ordersitems contains a row of data when i add item to basket...
Posted Image

Posted Image

Below it the coding for addtobasket.php, showcart.php, functions.php

addtobasket.php
<?php
session_start();

require("db.php");
require("functions.php");

$validid = pf_validate_number($_GET['id'],"redirect", $config_basedir);

$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$prodres = mysql_query($prodsql) or die(mysql_error());
$numrows = mysql_num_rows($prodres) or die(mysql_error());
$prodrow = mysql_fetch_assoc($prodres) or die(mysql_error());

if($numrows == 0)
{
	header("Location: " . $config_basedir);
}
else
{
	if($_POST['submit'])
	{
		if($_SESSION['SESS_ORDERNUM'])
		{
		$itemsql = "INSERT INTO orderitems(order_id,product_id, quantity) VALUES("
		. $_SESSION['SESS_ORDERNUM'] . ", ". $_GET['id'] . ", ". $_POST['amountBox'] . ")";
		mysql_query($itemsql) or die(mysql_error());
		}
		else
		{
			if($_SESSION['SESS_LOGGEDIN'])
				{
					$sql = "INSERT INTO orders(customer_id,registered, date) VALUES("
					. $_SESSION['SESS_USERID'] . ", 1, NOW())";
					mysql_query($sql) or die(mysql_error());
					session_register("SESS_ORDERNUM");
					$_SESSION['SESS_ORDERNUM'] = mysql_insert_id() or die(mysql_error());
					$itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES("
					. $_SESSION['SESS_ORDERNUM']. ", " . $_GET['id'] . ", "
					. $_POST['amountBox'] . ")";
					mysql_query($itemsql) or die(mysql_error());
				}
			else
				{
					$sql = "INSERT INTO orders(registered,date, session) VALUES("
					. "0, NOW(), '" . session_id() . "')";
					mysql_query($sql) or die(mysql_error());
					session_register("SESS_ORDERNUM");
					$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
					$itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES("
					. $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", "
					. $_POST['amountBox'] . ")";
					mysql_query($itemsql) or die(mysql_error());
				}
		}
		
		$totalprice = $prodrow['price'] * $_POST['amountBox'] ;
		$updsql = "UPDATE orders SET total = total + "
		. $totalprice . " WHERE id = "
		. $_SESSION['SESS_ORDERNUM'] . ";";
		mysql_query($updres) or die(mysql_error());
		header("Location: " . $config_basedir . "showcart.php");
}	
else
{
require("header.php");
echo "<form action='addtobasket.php?id="
. $_GET['id'] . "' method='POST'>";
echo "<table cellpadding='10'>";
echo "<tr>";
if(empty($prodrow['image'])) {
echo "<td><img
src='./productimages/dummy.jpg' width='50' alt='"
. $prodrow['name'] . "'></td>";
}
else {
echo "<td>
<img src='./productimages/" . $prodrow['image']
. "' width='50' alt='" . $prodrow['name']
. "'></td>";
}
echo "<td>" . $prodrow['name'] . "</td>";
echo "<td>Select Quantity <select name='amountBox'>";
for($i=1;$i<=5;$i++)
{
echo "<option>" . $i . "</option>";
}
echo "</select></td>";
echo "<td><strong>RM"
. sprintf('%.2f', $prodrow['price'])
. "</strong></td>";
echo "<td><input type='submit'
name='submit' value='Add to basket'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
}
require("footer.php");
?>



showcart.php
<?php
session_start();

require("header.php");
require("functions.php");

echo "<h1>Your shopping cart</h1>";
showcart();

if(isset($_SESSION['SESS_ORDERNUM']) == TRUE) 
{
	$sql = "SELECT * FROM orderitems WHERE
	order_id = " . $_SESSION['SESS_ORDERNUM'] . ";";
	$result = mysql_query($sql)or die(mysql_error());
	$numrows = mysql_num_rows($result)or die(mysql_error());
	
	if($numrows >= 1) 
		{
			echo "<h2><a href='checkout-address.php'>
			Go to the checkout</a></h2>";
		}
}

require("footer.php");
?>



functions.php
<?php
function pf_validate_number($value, $function, $redirect) {
if(isset($value) == TRUE) {
if(is_numeric($value) == FALSE) {
$error = 1;
}
if($error == 1) {
header("Location: " . $redirect);
}
else {
$final = $value;
}
}
else {
if($function == 'redirect') {
header("Location: " . $redirect);
}
if($function == "value") {
$final = 0;
}
}
return $final;
}

//showcart() function
function showcart()
{
	if($_SESSION['SESS_ORDERNUM'])
		{
			if($_SESSION['SESS_LOGGEDIN'])
				{
					$custsql = "SELECT id, status from
					orders WHERE customer_id = "
					. $_SESSION['SESS_USERID']
					. " AND status < 2;"; 
					$custres = mysql_query($custsql)or die(mysql_error());
					$custrow = mysql_fetch_assoc($custres)or die(mysql_error());
					$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS
					itemid FROM products, orderitems WHERE orderitems.product_id =
					products.id AND order_id = " . $custrow['id'];
					$itemsres = mysql_query($itemssql)or die(mysql_error());
					$itemnumrows = mysql_num_rows($itemsres)or die(mysql_error());
				}
					
			else
				{
					$custsql = "SELECT id, status from orders
					WHERE session = '" . session_id()		
					. "' AND status < 2;"; 
					$custres = mysql_query($custsql)or die(mysql_error());
					$custrow = mysql_fetch_assoc($custres)or die(mysql_error());
					$itemssql = "SELECT products.*,
					orderitems.*, orderitems.id AS itemid
					FROM products, orderitems WHERE
					orderitems.product_id = products.id AND
					order_id = " . $custrow['id'];
					$itemsres = mysql_query($itemssql)or die(mysql_error());
					$itemnumrows = mysql_num_rows($itemsres)or die(mysql_error());
					
				}
		}
	else
		{
			$itemnumrows = 0;
		}
					
		if($itemnumrows == 0)
			{
				echo "You have not added anything to your shopping cart yet.";
			}			
					
		else
			{
				echo "<table cellpadding='10'>";
				echo "<tr>";
				echo "<td></td>";
				echo "<td><strong>Item</strong></td>";
				echo "<td><strong>Quantity</strong></td>";
				echo "<td><strong>Unit Price</strong></td>";
				echo "<td><strong>Total Price</strong></td>";
				echo "<td></td>";
				echo "</tr>";	
				
				while($itemsrow = mysql_fetch_assoc($itemsres)or die(mysql_error()))
				{
					$quantitytotal =
					$itemsrow['price'] * $itemsrow['quantity'];
					echo "<tr>";
					
					if(empty($itemsrow['image'])) 
					{
						echo "<td><img
						src='./productimages/dummy.jpg' width='50' alt='"
						. $itemsrow['name'] . "'></td>";
					}				
					else 
					{
						echo "<td><img src='./productimages/" .
						$itemsrow['image'] . "' width='50' alt='"
						. $itemsrow['name'] . "'></td>";
					}
					
					echo "<td>" . $itemsrow['name'] . "</td>";
					echo "<td>" . $itemsrow['quantity'] . "</td>";
					echo "<td><strong>&pound;"
					. sprintf('%.2f', $itemsrow['price'])
					. "</strong></td>";
					echo "<td><strong>&pound;"
					. sprintf('%.2f', $quantitytotal) . "</strong></td>";
					echo "<td>[<a href='"
					. $config_basedir . "delete.php?id="
					. $itemsrow['itemid'] . "'>X</a>]</td>";
					echo "</tr>";
					$total = $total + $quantitytotal;
					$totalsql = "UPDATE orders SET total = "
					. $total . " WHERE id = "
					. $_SESSION['SESS_ORDERNUM']; 
					$totalres = mysql_query($totalsql)or die(mysql_error());
				}
					echo "<tr>";
					echo "<td></td>";
					echo "<td></td>";
					echo "<td></td>";
					echo "<td>TOTAL</td>";
					echo "<td><strong>&pound;"
					. sprintf('%.2f', $total) . "</strong></td>";
					echo "<td></td>";
					echo "</tr>";
					echo "</table>";
					echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
			}
}	
					
					
					
					
?>


Was This Post Helpful? 0
  • +
  • -

#11 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: showcart.php in Shopping Cart

Posted 07 March 2011 - 09:28 AM

On line 60 of addtobasket.php you a trying to run a SQL query ( $updres ) but I don't see it being declared anywhere.

Thus producing a empty query.
Since there is a mysql_error (the empty query) the page dies/hangs because this line.
mysql_query($updres) or die(mysql_error());



Try This:
There is a query in the variable $updsql, is that what you meant to run?

This post has been edited by VolcomMky: 07 March 2011 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#12 didi2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Re: showcart.php in Shopping Cart

Posted 08 March 2011 - 07:37 AM

mysql_query($updsql) or die(mysql_error());



I've changed above statement for the line 60... Able to direct to showcart.php show, but show empty item...By right It should display the cart content...
Anyway, at least able to find an error...
Posted Image
Was This Post Helpful? 0
  • +
  • -

#13 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: showcart.php in Shopping Cart

Posted 08 March 2011 - 08:44 AM

Does your query work as expected?

$updsql = "UPDATE orders SET total = total + "
        . $totalprice . " WHERE id = "
        . $_SESSION['SESS_ORDERNUM'] . ";";


If not try
$updsql = "UPDATE orders SET total=SUM(total+".$totalprice.") WHERE id=".$_SESSION['SESS_ORDERNUM'];


You might need to pull the current "total" as well so you can add it to the total price in the query
Was This Post Helpful? 0
  • +
  • -

#14 didi2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Re: showcart.php in Shopping Cart

Posted 08 March 2011 - 08:51 AM

View PostVolcomMky, on 08 March 2011 - 08:44 AM, said:

Does your query work as expected?

$updsql = "UPDATE orders SET total = total + "
        . $totalprice . " WHERE id = "
        . $_SESSION['SESS_ORDERNUM'] . ";";


If not try
$updsql = "UPDATE orders SET total=SUM(total+".$totalprice.") WHERE id=".$_SESSION['SESS_ORDERNUM'];


You might need to pull the current "total" as well so you can add it to the total price in the query

After changed to your coding, error "Invalid use of group function" shown under addtobasket.php page...

I wonder whether i can perform query under phpmyadmin to test each of it?

This post has been edited by didi2005: 08 March 2011 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#15 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: showcart.php in Shopping Cart

Posted 08 March 2011 - 08:57 AM

Yea that'd be great to test.
Cause if your query is running and you are being redirected to the cart it could be adding the item to a different session id, so your a step closer :bigsmile:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2