I'm currently working on a PHP shopping cart project in web development. A problem I have yet to resolve is passing the values of the product that I select from my product_list.php to cart.php - no matter what product I click, the name of the last product found in my products table is the one that keeps on appearing on the cart.php page.
For example, I would select Bamboo Mat, which is id no. 2, but the product name that would appear in cart.php is Bamboo Fence instead, which is id no. 8. Also, the value that appears in the quantity column is the number of remaining items of the selected product.
Only the values in the Product and Price columns are appearing as they should.
Here's the product_list.php file:
<?php
$err_level = error_reporting(0);
$conn = mysql_connect('params');
error_reporting($err_level);
?>
<?php
include "storescripts/connect_to_mysql.php";
?>
<html>
<head>
<title>Product List | Bamboo Art</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container">
<div class="header">
Bamboo Art
</div>
<div id="nav">
<ul id="nav">
<li><a href="index.php">Home</a>
</li><!--
--><li><a href="#" class="current">Products</a>
</li><!--
--><li><a href="#">Contact Us</a>
</li>
</ul>
</div>
<div class="content_product-list">
<form method="POST" action="addtocart.php">
<?php
$sql = "SELECT * FROM products";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
?>
<div class="products">
<input type='hidden' name='id' value=' <?php echo $row['id'] ?> '>
<?php echo "<img src='". $row['image'] ."' width=290 height=220>" ?><br><br>
<h3><?php echo $row['name'] . '<input type="hidden" name="name" value="' . $row['name'] . '">' ?></h3>
<h4><?php echo $row['description'] . '<input type="hidden" name="description" value="' . $row['description'] . '">' ?></h4>
Price: Php <?php echo $row['price'] . '<input type="hidden" name="price" value="' . $row['price'] . '">' ?><br>
<?php echo " Availability: " . $row['qty'] . " <br/>" ?>
<?php echo " Quantity: <select name='qty' style='width:40px;'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select> "?>
<br><br>
<input type='submit' value='Add to Cart' name='submit'> <br/>
</div>
<?php
}
?>
</form>
<br/>
</div>
</div>
</div>
</div>
</body>
</html>
The addtocart.php:
<?php
include "storescripts/connect_to_mysql.php";
if (isset($_POST['submit'])) {
$qty=$_REQUEST['qty'];
$name=$_REQUEST['name'];
$price=$_REQUEST['price'];
$id=$_REQUEST['id'];
$rs=mysql_query("SELECT * FROM cart WHERE prod_id='$id'",$con) or die (mysql_error());
if (mysql_num_rows($rs)>0 )
{
mysql_query("UPDATE cart SET qty=(qty+'$qty') WHERE prod_id='$id'",$con) or die (mysql_error());
mysql_query("UPDATE sales_record SET qty=(qty+'$qty') WHERE product_name='$name'",$con);
}
else
{
mysql_query("INSERT INTO cart (`cart_id`, `prod_id`, `qty`, `name`, `price`) VALUES (NULL, '$id', '$qty', '$name', '$price');",$con) or die(mysql_error());
mysql_query("INSERT INTO sales_record (`sales_id`, `customer_id`, `product_name`, `qty`,`price`) VALUES (NULL, '', '$name', '$qty', '$price');",$con);
}
mysql_query("UPDATE products SET qty=(qty-'$qty') WHERE id='$id'",$con) or die(mysql_error());
header("location:cart.php");
exit;
}
?>
The cart.php file:
<?php
include "storescripts/connect_to_mysql.php";
if(isset($_POST['checkout'])){
header("location:orders.php");
}
if(isset($_POST['shop'])){
header("location:product_list.php");
}
?>
<html>
<head>
<title>Shopping Cart | Bamboo Art</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container">
<div class="header">
Bamboo Art
</div>
<div id="nav">
<ul id="nav">
<li><a href="index.php">Home</a>
</li><!--
--><li><a href="product_list.php">Products</a>
</li><!--
--><li><a href="contactus.html">Contact Us</a>
</li>
</ul>
</div>
<div class="content">
<div class="table_content">
<h2>Your Cart</h2>
<form method="post">
<table id="table" align="center" border="0" width="100%">
<th>Qty</th>
<th>Product</th>
<th>Price</th>
<th>Total</th>
<tr>
<?php
//extract all items from the temporary cart
$q="SELECT * FROM cart, products WHERE cart.prod_id=products.id";
$rs=mysql_query($q) or die (mysql_error());
$total=0;
while($row=mysql_fetch_array($rs)){
echo '<td>'. $row['qty'] .'<input type="hidden" name="qty" value="' . $row['qty'] . '"></td>';
echo '<td>'. $row['name'] .'<input type="hidden" name="name" value="' . $row['name'] . '"></td>';
echo '<td>'. number_format($row['price'],'2','.',',') .'<input type="hidden" name="price" value="' . number_format($row['price'],'2','.',',') . '"></td>';
echo '<td><b>' . number_format(($row['qty']*$row['price']),'2','.',',') . '</b><input type="hidden" name="total" value="' . number_format(($row['qty']*$row['price']),'2','.',',') . '"></td>';
$total+=($row['qty']*$row['price']);
echo '</tr>';
echo '<tr>';
}
?>
<tr></tr>
<tr></tr>
<tr><td colspan="3"><strong>GRAND TOTAL</strong></td>
<td><?php echo number_format($total,'2','.',',');?><input type="hidden" name="gtotal" value="<?php echo number_format($total,'2','.',',');?>"></td>
</tr>
<tr height="40px"></tr>
<tr>
<td></td><td><input type="submit" name="checkout" value="Check Out" /></td>
<td><input type="submit" name="shop" value="Back to Shopping" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
I've had a look at a few shopping cart sample codes, trying to see if there is a solution to this, but it has just stumped me.
I did find this question which sounds similar to the problem that I have, but I can't use Javascript in building this shopping cart - just HTML, CSS and PHP.
I'm currently using PHP version 4.2.7.1.