问题描述
我已经使用PHP创建了购物车页面。现在我遇到的问题是,当我从产品列表中将产品添加到购物车时,发生的是仅添加了列表中的第一个产品。
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.
我错过了什么吗?
这是我的代码:
产品列表:
<?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.
添加到购物车:
<?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()
?>
显示购物车:
<?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);
}
?>
我怀疑错误是在产品列表代码上,但是我也检查了AddtoCart文件。
I suspect that the mistake is on the Product List code, but I have the AddtoCart file checked also.
推荐答案
已修复。错误(逻辑)来自用于更新shoppingcart页面的javascript。
Already fixed. The error (logical) was coming from a javascript used to update the shoppingcart page.
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
无论如何。谢谢您的回答。将为此发布一个问题。
Anyway. Thanks for your your answer guys. Will be posting a question in regards to this.
这篇关于PHP在购物车上传递价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!