I have created a shopping cart page using PHP. Now the problem I encountered was that, when I add a product to the cart from the product list, what happens is that only the 1st product on the list is added.
When I add another product (since every product on the list has it's own Add to Cart button), the 1st product is added again. Doesn't matter what item I choose, it still end up with the 1st product being added.
Am I missing something?
Here's my code:
Product List:
<?php do { ?>
<tr>
<td colspan="2"><font face="times new roman" size="3"><center><?php echo $prorow['pname']; ?></td>
<td colspan="1"><font face="times new roman" size="3"><center><?php echo $prorow['pdesc']; ?></td>
<td colspan="1"><font face="times new roman" size="3"><center><?php echo $prorow['price']; ?></td>
<td colspan="1"><center><img src="admin/<?php echo $prorow['image']; ?>" width="80" height="80" />
<td colspan="1">
<input type="submit" name="addtocart" value="Add to Cart">
</td>
</tr>
<?php } while ($prorow = mysqli_fetch_assoc($result)); ?>
I am passing values using hidden input types.
Add to Cart:
<?php
ob_start();
$con = mysqli_connect('localhost', 'abra', 'abra','abra') or die("Could not connect database");
$cname = mysql_escape_string($_POST['user']);
$pid=mysql_escape_string($_POST['proID']);
$pname=mysql_escape_string($_POST['proName']);
$price=mysql_escape_string($_POST['proPRICE']);
$qty=mysql_escape_string($_POST['qty']);
$addtocart = "INSERT INTO cart_track (bid, cName, pname, price, qty) VALUES ('$pid', '$cname', '$pname', '$price', '$qty')";
mysqli_query($con,$addtocart);
header("location:showcart.php");
exit;
ob_end_flush()
?>
Show Cart:
<?php
$con = mysqli_connect('localhost', 'abra', 'abra','abra') or die("Could not connect database");
//Check if user wants to checkout or shop:
if(isset($_POST['checkout']))
{
header("location:orders.php");
}
if(isset($_POST['shop']))
{
header("location:prodtable.php");
}
//retrieve items . use session_id and/or datetime
//$PHPSESSID=session_id();
$showcart = "SELECT * from cart_track INNER JOIN products ON bid=pId WHERE bid=pId";
$result=mysqli_query($con, $showcart);
if(!$result)
{
$err=true;
//i recommend writing this error to a log or some text file, for security reasons.
$errmsg=mysql_error();
}
else
{
$err=false;
$num=mysqli_num_rows($result);
}
?>
I suspect that the mistake is on the Product List code, but I have the AddtoCart file checked also.
mysqli
andmysql
, this cannot be done. Choose one or the other (preferablymysqli
overmysql
) – ʰᵈˑ