I’ve managed to get one stock table to display in the cart page when the user selects an item to purchase but I can’t figure out how to get more than one table from different pages to display.
Any help would be appreciated!
<?php
session_start();
$cart=$_SESSION["cart"];
extract($_POST);
if (IsSet($productid)||IsSet($_SESSION["notempty"])) $empty=false;
else $empty=true;
if (!$empty)
{
if (IsSet($cart[$productid])) $cart[$productid]+=$quantity;
else $cart[$productid]=$quantity;
$_SESSION["notempty"]=true;
}
$_SESSION["cart"]=$cart;
$conn=mysql_connect("","");
mysql_select_db("webent",$conn);
mysql_query("CREATE TABLE Stock (stock_ID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
itemName VARCHAR(50),itemDesc VARCHAR(100),
itemImage VARCHAR(10),itemStock VARCHAR(3),
itemPrice VARCHAR(5))");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<title>Jus' Books</title>
<link href="style2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container">
<div id="masthead">
</div>
<div id="navigation">
<p><a href="index1.php">Home</a>
<a href="fiction.php">Fiction</a>
<a href="travel.php">Travel</a>
<a href="sport.php">Sport</a>
<a href="arts.php">Arts & Design</a>
<a href="cart.php">Shopping Cart</a>
<a href="account.php">Manage Account</a></p>
</div>
<div id="content">
<?php
if ($empty) echo "<h4>Your shopping cart is empty</h4>";
else
{
echo "<h4>Your shopping cart contains:</h4>";
$total="";
foreach ($cart as $id=>$no)
{
if ($id!="")
{
$result=mysql_query("SELECT * FROM Stock WHERE stock_ID= $id");
$row=mysql_fetch_array($result);
extract($row);
if ($itemStock<$no) $no=$itemStock;
echo "<p>$no units of item $itemName at $itemPrice each</p>";
}
$total+=$no*$itemPrice;
}
echo "<p>Total to pay: £$total</p>";
}
?>
<a href="index1.php">Continue Shopping</a>
<a href="checkout.php">Proceed to Checkout</a>
<p></p>
</div>
<div id="footer">
<p><a href="index1.php">Home</a> |
<a href="fiction.php">Fiction</a> |
<a href="travel.php">Travel</a> |
<a href="sport.php">Sport</a> |
<a href="arts.php">Arts & Design</a> |
<a href="cart.php">Shopping Cart</a> |
<a href="account.php">Manage Account</a></p>
</div>
</div>
</body>
</html>
The other tables have the exact same fields but have the name Stock_Travel
Stock_Sport
Stock_Arts
What do you mean by that? If you want to select from different tables, alter the query:
I hope this is some kind of homework assignment, since it’s full of tiny flaws that wouldn’t be nice in a production environment.
As you see, the database layout is far from optimal. Why isn’t Travel/Sport/Arts stored in a
Categoryfield in the Stock table? That way you don’t have to alter your query based on the kind of product page you’re on.Furthermore, I wouldn’t rely on
extract(), but that’s personal. You definitely will want to escape your queries, or rather use prepared statements.