Page 1 of 2

checkout problem

Posted: Thu Feb 24, 2005 7:49 pm
by elle_girl
cart.php

Code: Select all

<?php

	include("db.php");
		
	switch($_GET&#1111;"action"])
	&#123;
		case "add_item":
		&#123;
			AddItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "update_item":
		&#123;
			UpdateItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "remove_item":
		&#123;
			RemoveItem($_GET&#1111;"id"]);
			ShowCart();
			break;
		&#125;
		default:
		&#123;
			ShowCart();
		&#125;
	&#125;

	function AddItem($itemId, $qty)
	&#123;
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		$row = mysql_fetch_row($result);
		$numRows = $row&#1111;0];
		
		if($numRows == 0)
		&#123;
			// This item doesn't exist in the users cart,
			// we will add it with an insert query

			@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
		&#125;
		else
		&#123;
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($itemId, $qty);
		&#125;
	&#125;
	
	function UpdateItem($itemId, $qty)
	&#123;
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		if($qty == 0)
		&#123;
			// Remove the item from the users cart
			RemoveItem($itemId);
		&#125;
		else
		&#123;
			mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		&#125;
	&#125;
	
	function RemoveItem($itemId)
	&#123;
		// Uses an SQL delete statement to remove an item from
		// the users cart

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
	&#125;
	
	function ShowCart()
	&#123;
		// Gets each item from the cart table and display them in
		// a tabulated format, as well as a final total for the cart
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		$totalCost = 0;
		$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
		$result1 = mysql_query("insert into view (itemId) VALUES ($itemId)");
		
		?>
		<html>
		<head>
		<title> Your Shopping Cart </title>
		<script language="JavaScript">
		
			function UpdateQty(item)
			&#123;
				itemId = item.name;
				newQty = item.options&#1111;item.selectedIndex].text;
				
				document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
			&#125;
		
		</script>
		</head>
		<body bgcolor="#ffffff">
		<h1>Your Shopping Cart</h1>
		<form name="frmCart" method="get">
		<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="15%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						&nbsp;&nbsp;<b>Qty</b>
					</font>
				</td>
				<td width="55%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Product</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Price Each</b>
					</font>
				</td>
				<td width="10%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Remove?</b>
					</font>
				</td>
			</tr>
			<?php
			
			while($row = mysql_fetch_array($result))
			&#123;
				// Increment the total cost of all items
				$totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
				?>
					<tr>
						<td width="15%" height="25">
							<font face="verdana" size="1" color="black">
								<select name="<?php echo $row&#1111;"itemId"]; ?>" onChange="UpdateQty(this)">
								<?php
								
									for($i = 1; $i <= 20; $i++)
									&#123;
										echo "<option ";
										if($row&#1111;"qty"] == $i)
										&#123;
											echo " SELECTED ";
										&#125;
										echo ">" . $i . "</option>";
									&#125;
								?>
								</select>
							</font>
						</td>
						<td width="55%" height="25">
							<font face="verdana" size="1" color="black">
								<?php echo $row&#1111;"itemName"]; ?>
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								$<?php echo number_format($row&#1111;"itemPrice"], 2, ".", ","); ?>
							</font>
						</td>
						<td width="10%" height="25">
							<font face="verdana" size="1" color="black">
								<a href="cart.php?action=remove_item&id=<?php echo $row&#1111;"itemId"]; ?>">Remove</a>
							</font>
						</td>
					</tr>
				<?php
			&#125;
			
			// Display the total
			?>
					<tr>
						<td width="100%" colspan="4">
							<hr size="1" color="red" NOSHADE>
						</td>
					</tr>
					<tr>
						<td width="70%" colspan="2">
							<font face="verdana" size="1" color="black">
								<a href="products.php"><< Keep Shopping</a>
							</font>
						</td>
						<td width="70%" colspan="2">
							<font face="verdana" size="1" color="black">
								<a href="checkout.php"><< Continue Shopping</a>
							</font>
						</td>
						<td width="30%" colspan="2">
							<font face="verdana" size="2" color="black">
								<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
							</font>
						</td>
					</tr>
				</table>
				</form>
			</body>
			</html>
			<?php
	&#125;

?>
When I click the link continue shopping. I want the data that I stored in cart database to be appear on the next page that I called checkout.php

Code: Select all

<?php
// This is the registration page for the site.


// Set the page title and include the HTML header.
$page_title = 'Confirmation';


if (isset($_POST&#1111;'submit'])) &#123; // Handle the form.

	require_once ('./mysql_connect.php'); // Connect to the db
	
	// Create a function for escaping the data.
	function escape_data ($data) &#123;
		global $dbc; // Need the connection.
		if (ini_get('magic_quotes_gpc')) &#123;
			$data = stripslashes($data);
		&#125;
		return mysql_real_escape_string($data, $dbc);
	&#125; // End of function.
	
	

	$message = NULL; // Create an empty new variable.

	

	// Check for the account number.
	if (empty($_POST&#1111;'account_no'])) &#123;
		$aa = FALSE;
		$message .= '<p>You forgot to enter your account number!</p>';
	&#125; else &#123;
		$aa = escape_data($_POST&#1111;'account_no']);
	&#125;

	if (empty($_POST&#1111;'token'])) &#123;
		$t = FALSE;
		$message .= '<p>You forgot to enter your reference ID!</p>';
	&#125; else &#123;
		$t = escape_data($_POST&#1111;'token']);
	&#125;

	if (empty($_POST&#1111;'name'])) &#123;
		$n = FALSE;
	&#125; else &#123;
		$n = escape_data($_POST&#1111;'name']);
	&#125;

	if (empty($_POST&#1111;'address'])) &#123;
		$ad = FALSE;
		
	&#125; else &#123;
		$ad = escape_data($_POST&#1111;'address']);
	&#125;

	if (empty($_POST&#1111;'postcode'])) &#123;
		$p = FALSE;
		
	&#125; else &#123;
		$p = escape_data($_POST&#1111;'postcode']);
	&#125;

	if (empty($_POST&#1111;'state'])) &#123;
		$s = FALSE;
		
	&#125; else &#123;
		$s = escape_data($_POST&#1111;'state']);
	&#125;

	if (empty($_POST&#1111;'country'])) &#123;
		$c = FALSE;
		
	&#125; else &#123;
		$c = escape_data($_POST&#1111;'country']);
	&#125;

	
	

	if ( $aa && $t ) &#123; // If everything's OK.

		

		
		// Make sure the token available.
		$query = "SELECT * FROM customer_bank WHERE token_id = '$t' ";
		$result = @mysql_query ($query);

		
		
		
		if ($result && $result1) &#123; 
    			// Add the user. 
    			 $query = "INSERT INTO view (account_no, token, name, address, postcode, state, country) VALUES ('$aa','$t','$n','$ad','$p','$s','$c' )"; 
     			$result = @mysql_query ($query); // Run the query. 
   			 // Retrieve all of the information for the prints in the cart. 

			
			

			// Successful add the new customer.
			echo '<h3>Successful the transaction</h3>';
			include ('includes/header_bank.html');
			exit();

			
		&#125; else &#123; // The account number does not match with token ID.
			echo '<p><font color="red" size="+1">The account number does not match with the reference ID in the database</font></p>';
		&#125;


			
			
		mysql_close(); // Close the database connection.

	&#125; else &#123; // If it did not run OK.
		$message = '<p>Please try again.</p>';
	&#125;

&#125; // End of the main Submit conditional.

// Print the error message if there is one.
if (isset($message)) &#123;
	echo '<font color="red">', $message, '</font>';
&#125;
?>
	
<form action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>"method="post">
<fieldset><legend>Enter the reference id that you get request from the server with the account number that register with this website:</legend>

<p><b>Account number:</b> <input type="text" name="account_no" size="50" maxlength="50" value="<?php if (isset($_POST&#1111;'account_no'])) echo $_POST&#1111;'account_no']; ?>" /></p>

<p><b>Reference ID:</b> <input type="text" name="token" size="20" maxlength="20" value="<?php if (isset($_POST&#1111;'token'])) echo $_POST&#1111;'token']; ?>" /></p>

</fieldset>

<fieldset><legend>Enter the billing information correctly so that the product can delivered</legend>

<p><b>Recipient Name:</b> <input type="text" name="name" size="20" maxlength="20" value="<?php if (isset($_POST&#1111;'name'])) echo $_POST&#1111;'name']; ?>" /></p>

<p><b>Address:</b> <input type="text" name="address" size="50" maxlength="50" value="<?php if (isset($_POST&#1111;'address'])) echo $_POST&#1111;'address']; ?>" /></p>

<p><b>Postcode:</b> <input type="text" name="postcode" size="5" maxlength="5" value="<?php if (isset($_POST&#1111;'postcode'])) echo $_POST&#1111;'postcode']; ?>" /></p>

<p><b>State:</b> <input type="text" name="state" size="10" maxlength="10" value="<?php if (isset($_POST&#1111;'state'])) echo $_POST&#1111;'state']; ?>" /></p>	

<p><b>Country:</b> <input type="text" name="country" size="10" maxlength="10" value="<?php if (isset($_POST&#1111;'country'])) echo $_POST&#1111;'country']; ?>" /></p>

</fielset>
<div align="center"><input type="submit" name="submit" value="Confirmation" /></div>

</form><!-- End of Form -->

<?php
include ('includes/footer_home.html');
?>

I also want the data in the cart to be insert into the database view. Can u help me to do the coding for this part. I has no idea to do it.

Posted: Thu Feb 24, 2005 8:08 pm
by smpdawg
I don't mean to complain but isn't this the same problem that you posted the other day? Why would you start another thread on the same topic?

Posted: Thu Feb 24, 2005 8:13 pm
by feyd
sooo... perform the query that creates $result in cart.php::ShowCart() what's the problem?

Posted: Thu Feb 24, 2005 9:26 pm
by elle_girl
The problem is that it cannot add the query.

It appear this problem

Notice: Undefined variable: itemId in C:\Program Files\Apache Group\Apache2\htdocs\cart.php on line 111

їcode]
$result1 = mysql_query("insert into view (itemId) VALUES ($itemId)");
ї/code]

Posted: Thu Feb 24, 2005 9:32 pm
by smpdawg
$itemId is not in scope in your ShowCart function. Where was item coming from? In your other functions you passed it in but here it seems to be coming from the DB. If so, you need to get it from the previous query.

Posted: Thu Feb 24, 2005 11:21 pm
by elle_girl
If I want to get it from previous queries in the table cart. How can I select from the table cart to insert into table view. Can u please tell me to do the sql queries?

Posted: Thu Feb 24, 2005 11:59 pm
by smpdawg
From the looks of your code you could move this line

Code: Select all

$result1 = mysql_query("insert into view (itemId) VALUES ($itemId)");
into this block of code

Code: Select all

while($row = mysql_fetch_array($result))
         &#123;
            // Increment the total cost of all items
            $totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);

            $itemId = $row&#1111;'itemId'];
            $result1 = mysql_query("insert into view (itemId) VALUES ($itemId)"); 

            ?>
               <tr>
Notice that I also added the line that retrieves itemid from the first query. Now that it is in the while loop, it should process the itemid for each entry in the cart.

Posted: Fri Feb 25, 2005 8:54 am
by elle_girl
I make some modification to my script.

cart.php

Code: Select all

<?php

	include("db.php");
		
	switch($_GET&#1111;"action"]) // line 5
	&#123;
		case "add_item":
		&#123;
			AddItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "update_item":
		&#123;
			UpdateItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "remove_item":
		&#123;
			RemoveItem($_GET&#1111;"id"]);
			ShowCart();
			break;
		&#125;
		default:
		&#123;
			ShowCart();
		&#125;
	&#125;

	function AddItem($itemId, $qty)
	&#123;
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		$row = mysql_fetch_row($result);
		$numRows = $row&#1111;0];
		
		if($numRows == 0)
		&#123;
			// This item doesn't exist in the users cart,
			// we will add it with an insert query

			@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
		&#125;
		else
		&#123;
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($itemId, $qty);
		&#125;
	&#125;
	
	function UpdateItem($itemId, $qty)
	&#123;
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		if($qty == 0)
		&#123;
			// Remove the item from the users cart
			RemoveItem($itemId);
		&#125;
		else
		&#123;
			mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		&#125;
	&#125;
	
	function RemoveItem($itemId)
	&#123;
		// Uses an SQL delete statement to remove an item from
		// the users cart

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
	&#125;
	
	function ShowCart()
	&#123;
		// Gets each item from the cart table and display them in
		// a tabulated format, as well as a final total for the cart
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		$totalCost = 0;
		$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
		
		
		?>
		<html>
		<head>
		<title> Your Shopping Cart </title>
		<script language="JavaScript">
		
			function UpdateQty(item)
			&#123;
				itemId = item.name;
				newQty = item.options&#1111;item.selectedIndex].text;
				
				document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
			&#125;
		
		</script>
		</head>
		<body bgcolor="#ffffff">
		<h1>Your Shopping Cart</h1>
		<form name="frmCart" method="get">
		<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="15%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						&nbsp;&nbsp;<b>Qty</b>
					</font>
				</td>
				<td width="55%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Product</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Price Each</b>
					</font>
				</td>
				<td width="10%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Remove?</b>
					</font>
				</td>
			</tr>
			<?php
			
			while($row = mysql_fetch_array($result))
			&#123;
				// Increment the total cost of all items
				$totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
				
            

				?>
					<tr>
						<td width="15%" height="25">
							<font face="verdana" size="1" color="black">
								<select name="<?php echo $row&#1111;"itemId"]; ?>" onChange="UpdateQty(this)">
								<?php
								
									for($i = 1; $i <= 20; $i++)
									&#123;
										echo "<option ";
										if($row&#1111;"qty"] == $i)
										&#123;
											echo " SELECTED ";
										&#125;
										echo ">" . $i . "</option>";
									&#125;
								?>
								</select>
							</font>
						</td>
						<td width="55%" height="25">
							<font face="verdana" size="1" color="black">
								<?php echo $row&#1111;"itemName"]; ?>
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								$<?php echo number_format($row&#1111;"itemPrice"], 2, ".", ","); ?>
							</font>
						</td>
						<td width="10%" height="25">
							<font face="verdana" size="1" color="black">
								<a href="cart.php?action=remove_item&id=<?php echo $row&#1111;"itemId"]; ?>">Remove</a>
							</font>
						</td>
					</tr>
				<?php
			&#125;
			
			// Display the total
			?>
					<tr>
						<td width="100%" colspan="4">
							<hr size="1" color="red" NOSHADE>
						</td>
					</tr>
					<tr>
						<td width="70%" colspan="2">
							<font face="verdana" size="1" color="black">
								<a href="products.php"><< Keep Shopping</a>
							</font>
						</td>
						<td width="70%" colspan="2">
							<font face="verdana" size="1" color="black"><input type="submit" name="submit" value="Continue Shopping" /><br /><br />
								<a href="checkout.php"></a>
							</font>
						</td>
						 

						<td width="30%" colspan="2">
							<font face="verdana" size="2" color="black">
								<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
							</font>
						</td>
					</tr>
				</table>
				</form>
			</body>
			</html>
			<?php
	&#125;

?>
It appear this error.

Notice: Undefined index: action in C:\Program Files\Apache Group\Apache2\htdocs\cart.php on line 5

Can u please help me 2 solve this problem?

Posted: Fri Feb 25, 2005 8:59 am
by feyd
  1. change $_GET['action'] to $action.
  2. add the following immedately after the include call on line 3.

    Code: Select all

    $action = (isset($_GET&#1111;'action']) ? $_GET&#1111;'action'] : '');

Posted: Fri Feb 25, 2005 12:53 pm
by smpdawg
What happened to making this change?

Code: Select all

while($row = mysql_fetch_array($result))
         &#123;
            // Increment the total cost of all items
            $totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);

            $itemId = $row&#1111;'itemId'];
            $result1 = mysql_query("insert into view (itemId) VALUES ($itemId)");

            ?>
               <tr>

Posted: Fri Feb 25, 2005 6:39 pm
by elle_girl
I think I does not want the insert itemId. Because I think it does not have any useful for this shopping cart.

So I make a little modification to the code

cart.php

Code: Select all

<?php

	include("db.php");
		
	switch($action = (isset($_GET&#1111;'action']) ? $_GET&#1111;'action'] : ''))
	&#123;
		case "add_item":
		&#123;
			AddItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "update_item":
		&#123;
			UpdateItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "remove_item":
		&#123;
			RemoveItem($_GET&#1111;"id"]);
			ShowCart();
			break;
		&#125;
		default:
		&#123;
			ShowCart();
		&#125;
	&#125;

	function AddItem($itemId, $qty)
	&#123;
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		$row = mysql_fetch_row($result);
		$numRows = $row&#1111;0];
		
		if($numRows == 0)
		&#123;
			// This item doesn't exist in the users cart,
			// we will add it with an insert query

			@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
		&#125;
		else
		&#123;
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($itemId, $qty);
		&#125;
	&#125;
	
	function UpdateItem($itemId, $qty)
	&#123;
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		if($qty == 0)
		&#123;
			// Remove the item from the users cart
			RemoveItem($itemId);
		&#125;
		else
		&#123;
			mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		&#125;
	&#125;
	
	function RemoveItem($itemId)
	&#123;
		// Uses an SQL delete statement to remove an item from
		// the users cart

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
	&#125;
	
	function ShowCart()
	&#123;
		// Gets each item from the cart table and display them in
		// a tabulated format, as well as a final total for the cart
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		$totalCost = 0;
		$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
		
		
		?>
		<html>
		<head>
		<title> Your Shopping Cart </title>
		<script language="JavaScript">
		
			function UpdateQty(item)
			&#123;
				itemId = item.name;
				newQty = item.options&#1111;item.selectedIndex].text;
				
				document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
			&#125;
		
		</script>
		</head>
		<body bgcolor="#ffffff">
		<h1>Your Shopping Cart</h1>
		<form name="frmCart" method="get">
		<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="15%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						&nbsp;&nbsp;<b>Qty</b>
					</font>
				</td>
				<td width="55%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Product</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Price Each</b>
					</font>
				</td>
				<td width="10%" height="25" bgcolor="red">
					<font face="verdana" size="1" color="white">
						<b>Remove?</b>
					</font>
				</td>
			</tr>
			<?php
			
			while($row = mysql_fetch_array($result))
			&#123;
				// Increment the total cost of all items
				$totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
				
            

				?>
					<tr>
						<td width="15%" height="25">
							<font face="verdana" size="1" color="black">
								<select name="<?php echo $row&#1111;"itemId"]; ?>" onChange="UpdateQty(this)">
								<?php
								
									for($i = 1; $i <= 20; $i++)
									&#123;
										echo "<option ";
										if($row&#1111;"qty"] == $i)
										&#123;
											echo " SELECTED ";
										&#125;
										echo ">" . $i . "</option>";
									&#125;
								?>
								</select>
							</font>
						</td>
						<td width="55%" height="25">
							<font face="verdana" size="1" color="black">
								<?php echo $row&#1111;"itemName"]; ?>
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								$<?php echo number_format($row&#1111;"itemPrice"], 2, ".", ","); ?>
							</font>
						</td>
						<td width="10%" height="25">
							<font face="verdana" size="1" color="black">
								<a href="cart.php?action=remove_item&id=<?php echo $row&#1111;"itemId"]; ?>">Remove</a>
							</font>
						</td>
					</tr>
				<?php
			&#125;
			
			// Display the total
			?>
					<tr>
						<td width="100%" colspan="4">
							<hr size="1" color="red" NOSHADE>
						</td>
					</tr>
					<tr>
						<td width="70%" colspan="2">
							<font face="verdana" size="1" color="black">
								<a href="products.php"><< Keep Shopping</a>
							</font>
						</td>
						<td width="70%" colspan="2">
							<font face="verdana" size="1" color="black">
								<a href="checkout.php"><< Continue Shopping</a>
							</font>
						</td>
						
						 

						<td width="30%" colspan="2">
							<font face="verdana" size="2" color="black">
								<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
							</font>
						</td>
					</tr>
				</table>
				</form>
			</body>
			</html>
			<?php
	&#125;

?>
checkout.php

Code: Select all

<?php
// This is the registration page for the site.

// Set the page title and include the HTML header.
$page_title = 'Confirmation';


if (isset($_POST&#1111;'action'])) &#123; // Handle the form.

	// Register the user in the database.
	require_once ('./mysql_connect.php'); // Connect to the db.

	// Create a function for escaping the data.
	function escape_data ($data) &#123;
		global $dbc; // Need the connection.
		if (ini_get('magic_quotes_gpc')) &#123;
			$data = stripslashes($data);
		&#125;
		return mysql_real_escape_string($data, $dbc);
	&#125; // End of function.

	$message = NULL; // Create an empty new variable.

	

	// Check for the account number.
	if (empty($_POST&#1111;'account_no'])) &#123;
		$aa = FALSE;
		$message .= '<p>You forgot to enter your account number!</p>';
	&#125; else &#123;
		$aa = escape_data($_POST&#1111;'account_no']);
	&#125;

	if (empty($_POST&#1111;'token_id'])) &#123;
		$t = FALSE;
		$message .= '<p>You forgot to enter your reference ID!</p>';
	&#125; else &#123;
		$t = escape_data($_POST&#1111;'token_id']);
	&#125;

	if (empty($_POST&#1111;'name'])) &#123;
		$n = FALSE;
	&#125; else &#123;
		$n = escape_data($_POST&#1111;'name']);
	&#125;

	if (empty($_POST&#1111;'address'])) &#123;
		$ad = FALSE;
		
	&#125; else &#123;
		$ad = escape_data($_POST&#1111;'address']);
	&#125;

	if (empty($_POST&#1111;'postcode'])) &#123;
		$p = FALSE;
		
	&#125; else &#123;
		$p = escape_data($_POST&#1111;'postcode']);
	&#125;

	if (empty($_POST&#1111;'state'])) &#123;
		$s = FALSE;
		
	&#125; else &#123;
		$s = escape_data($_POST&#1111;'state']);
	&#125;

	if (empty($_POST&#1111;'country'])) &#123;
		$c = FALSE;
		
	&#125; else &#123;
		$c = escape_data($_POST&#1111;'country']);
	&#125;

	
	

	if ( $aa && $t ) &#123; // If everything's OK.

		
		// Make sure the token available.
		$query = "SELECT * FROM customer_bank WHERE token_id = '$t' AND account_no = '$an' ";
		$result = @mysql_query ($query);
		
		if ($result) &#123; 
    		// Add the user. 
    		$query = "INSERT INTO view (account_no, token_id, name, address, postcode, state, country) VALUES ( '$aa','$t','$n','$ad','$p','$s','$c' )"; 
        	$result = @mysql_query ($query); // Run the query. 
   			 
			
			

			// Successful add the new customer.
			echo '<h3>Successful the transaction</h3>';
			include ('includes/header_bank.html');
			exit();

			
		&#125; else &#123; // The account number does not match with token ID.
			echo '<p><font color="red" size="+1">The account number does not match with the reference ID in the database</font></p>';
		&#125;
    &#125;

			
			
		mysql_close(); // Close the database connection.

	

&#125; // End of the main Submit conditional.

// Print the error message if there is one.
if (isset($message)) &#123;
	echo '<font color="red">', $message, '</font>';
&#125;
?>
	
<form action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>"method="post">
<fieldset><legend>Enter the reference id that you get request from the server with the account number that register with this website:</legend>

<p><b>Account number:</b> <input type="text" name="account_no" size="50" maxlength="50" value="<?php if (isset($_POST&#1111;'account_no'])) echo $_POST&#1111;'account_no']; ?>" /></p>

<p><b>Reference ID:</b> <input type="text" name="token" size="20" maxlength="20" value="<?php if (isset($_POST&#1111;'token_id'])) echo $_POST&#1111;'token_id']; ?>" /></p>

</fieldset>

<fieldset><legend>Enter the billing information correctly so that the product can delivered</legend>

<p><b>Recipient Name:</b> <input type="text" name="name" size="20" maxlength="20" value="<?php if (isset($_POST&#1111;'name'])) echo $_POST&#1111;'name']; ?>" /></p>

<p><b>Address:</b> <input type="text" name="address" size="50" maxlength="50" value="<?php if (isset($_POST&#1111;'address'])) echo $_POST&#1111;'address']; ?>" /></p>

<p><b>Postcode:</b> <input type="text" name="postcode" size="5" maxlength="5" value="<?php if (isset($_POST&#1111;'postcode'])) echo $_POST&#1111;'postcode']; ?>" /></p>

<p><b>State:</b> <input type="text" name="state" size="10" maxlength="10" value="<?php if (isset($_POST&#1111;'state'])) echo $_POST&#1111;'state']; ?>" /></p>	

<p><b>Country:</b> <input type="text" name="country" size="10" maxlength="10" value="<?php if (isset($_POST&#1111;'country'])) echo $_POST&#1111;'country']; ?>" /></p>

</fielset>
<div align="center"><input type="submit" name="submit" value="Confirmation" /></div>

</form><!-- End of Form -->

<?php
include ('includes/footer_home.html');
?>
I don't know why when I enter the data at the checkout.php. It cannot insert the data to the database. Can u please help me to solve this problem.

Actually after we click the button at the checkout.php script I want the product that the customer want to buy will update at the table call confirm with the reference id and account number. How 2 do it? Please help me to do this in the coding part?

Posted: Fri Feb 25, 2005 6:48 pm
by smpdawg
Is there a site where we can see it in action? Sometimes it is easier to troubleshoot if we can it in action.

Posted: Fri Feb 25, 2005 7:10 pm
by elle_girl
I don't have that site.Sorry!Can u tell me any part of the coding that I need 2 change!

Posted: Sat Feb 26, 2005 1:16 am
by n00b Saibot
Remove the '@' from front of every mysql_query statement and add
or die(MySQL_Error())
to end of it.
that way you'll come to know if there are any errors in processing data.

Posted: Sun Feb 27, 2005 7:09 am
by elle_girl
I make a litle modification

Still appear the problem.

Cannot insert data to the database. But did not appear any error

Code: Select all

<?php
// This is the registration page for the site.

// Set the page title and include the HTML header.
$page_title = 'Confirmation';


if (isset($_GET&#1111;'action'])) &#123; // Handle the form.

	// Register the user in the database.
	require_once ('./mysql_connect.php'); // Connect to the db.

	// Create a function for escaping the data.
	function escape_data ($data) &#123;
		global $dbc; // Need the connection.
		if (ini_get('magic_quotes_gpc')) &#123;
			$data = stripslashes($data);
		&#125;
		return mysql_real_escape_string($data, $dbc);
	&#125; // End of function.

	$message = NULL; // Create an empty new variable.

	

	// Check for the account number.
	if (empty($_POST&#1111;'account_no'])) &#123;
		$aa = FALSE;
		$message .= '<p>You forgot to enter your account number!</p>';
	&#125; else &#123;
		$aa = escape_data($_POST&#1111;'account_no']);
	&#125;

	if (empty($_POST&#1111;'token_id'])) &#123;
		$t = FALSE;
		$message .= '<p>You forgot to enter your reference ID!</p>';
	&#125; else &#123;
		$t = escape_data($_POST&#1111;'token_id']);
	&#125;

	if (empty($_POST&#1111;'name'])) &#123;
		$n = FALSE;
	&#125; else &#123;
		$n = escape_data($_POST&#1111;'name']);
	&#125;

	if (empty($_POST&#1111;'address'])) &#123;
		$ad = FALSE;
		
	&#125; else &#123;
		$ad = escape_data($_POST&#1111;'address']);
	&#125;

	if (empty($_POST&#1111;'postcode'])) &#123;
		$p = FALSE;
		
	&#125; else &#123;
		$p = escape_data($_POST&#1111;'postcode']);
	&#125;

	if (empty($_POST&#1111;'state'])) &#123;
		$s = FALSE;
		
	&#125; else &#123;
		$s = escape_data($_POST&#1111;'state']);
	&#125;

	if (empty($_POST&#1111;'country'])) &#123;
		$c = FALSE;
		
	&#125; else &#123;
		$c = escape_data($_POST&#1111;'country']);
	&#125;

	
	

	if ( $aa && $t ) &#123; // If everything's OK.

		
		// Make sure the token available.
		$query = "SELECT * FROM customer_bank WHERE token_id = '$t' AND account_no = '$an' ";
		$result = @mysql_query ($query);
		$num=mysql_num_rows($result);
		
		if ($submit) &#123;
		if ($num!=0) &#123; 
    		// Add the user. 
    		$query2 = "INSERT INTO view (account_no, token_id, name, address, postcode, state, country) VALUES ( '$aa','$t','$n','$ad','$p','$s','$c' )"; 
        	mysql_query($query2) or die(mysql_error());
   			 
			
			

			// Successful add the new customer.
			echo '<h3>Successful the transaction</h3>';
			include ('includes/header_bank.html');
			exit();

			
		&#125; &#125; else &#123; // The account number does not match with token ID.
			echo '<p><font color="red" size="+1">The account number does not match with the reference ID in the database</font></p>';
		&#125;
    &#125;

			
			
		mysql_close(); // Close the database connection.

	

&#125; // End of the main Submit conditional.

// Print the error message if there is one.
if (isset($message)) &#123;
	echo '<font color="red">', $message, '</font>';
&#125;
?>
	
<form action="checkout.php" method="post">
<fieldset><legend>Enter the reference id that you get request from the server with the account number that register with this website:</legend>

<p><b>Account number:</b> <input type="text" name="account_no" size="50" maxlength="50" value="<?php if (isset($_POST&#1111;'account_no'])) echo $_POST&#1111;'account_no']; ?>" /></p>

<p><b>Reference ID:</b> <input type="text" name="token" size="20" maxlength="20" value="<?php if (isset($_POST&#1111;'token_id'])) echo $_POST&#1111;'token_id']; ?>" /></p>

</fieldset>

<fieldset><legend>Enter the billing information correctly so that the product can delivered</legend>

<p><b>Recipient Name:</b> <input type="text" name="name" size="20" maxlength="20" value="<?php if (isset($_POST&#1111;'name'])) echo $_POST&#1111;'name']; ?>" /></p>

<p><b>Address:</b> <input type="text" name="address" size="50" maxlength="50" value="<?php if (isset($_POST&#1111;'address'])) echo $_POST&#1111;'address']; ?>" /></p>

<p><b>Postcode:</b> <input type="text" name="postcode" size="5" maxlength="5" value="<?php if (isset($_POST&#1111;'postcode'])) echo $_POST&#1111;'postcode']; ?>" /></p>

<p><b>State:</b> <input type="text" name="state" size="10" maxlength="10" value="<?php if (isset($_POST&#1111;'state'])) echo $_POST&#1111;'state']; ?>" /></p>	

<p><b>Country:</b> <input type="text" name="country" size="10" maxlength="10" value="<?php if (isset($_POST&#1111;'country'])) echo $_POST&#1111;'country']; ?>" /></p>

</fielset>
<div align="center"><input type="submit" name="submit" value="Confirmation" /></div>

</form><!-- End of Form -->

<?php
include ('includes/footer_home.html');
?>