我有一个PHP页面,用于显示数据库表中的产品,同时显示它们,如果用户单击添加到购物车或添加到愿望列表,相应的表单提交,产品详细信息将插入到DB中的另一个表中,
虽然插入工作正常但是产品用户添加到购物车的任何内容只有表中的第一行才会插入。
我的代码如下:
索引:
<?php
$sql3 = "SELECT * FROM cakes ORDER BY date2 DESC LIMIT 30";
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
?>
<div class="bt-item-extra product-layout odd element-4">
<section class="product-thumb bt-item transition">
<div class="image">
<a href="individual.php?id=<?php echo $row3['id'];?>"><img src="images/product/<?php echo $row3['name'];?>.jpg" style="width:200px;" alt="Tasty Cakes" title="Tasty Cakes" class="img-responsive" />
</a>
<div class="button-group button-grid">
<form action="addtocart.php" method="post" id="addtocart"></form>
<form action="addtowishlist.php" method="post" id="addtowishlist"></form>
<input type="hidden" name="ip" value="<?php echo $localIP; ?>" form="addtocart" />
<input type="hidden" name="cake" value="<?php echo $row3['name'];?>" form="addtocart" />
<input type="hidden" name="price" value="<?php echo $row3['price'];?>" form="addtocart" />
<input type="hidden" name="ip" value="<?php echo $localIP; ?>" form="addtowishlist" />
<input type="hidden" name="cake" value="<?php echo $row3['name'];?>" form="addtowishlist" />
<input type="hidden" name="price" value="<?php echo $row3['price'];?>" form="addtowishlist" />
<button class="btn-cart" type="submit" form="addtocart"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
<button class="btn-wishlist" type="submit" form="addtowishlist" title="Add to Wish List"><i class="fa fa-heart"></i></button>
</div>
</div>
<div class="small_detail">
<div class="caption">
<div class="name"><a href="individual.php?id=<?php echo $row3['id'];?>"><?php echo $row3['name'];?></a>
</div>
<p class="price"> ₹<?php echo $row3['price'];?>.00 </p>
</div>
</div>
</section>
</div><!-- /.bt-item-extra -->
<?php }
} else {
echo "";
}
?>
Input.php
<?php
$servername = "localhost";
$dbusername = "uname";
$dbpassword = "pass";
$dbname = "dbname";
$cake = $_POST['cake'];
$price = $_POST['price'];
$quantity= '1';
$ip = $_POST['ip'];
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO cart (cake, price, ip, quantity, date2)
VALUES ('$cake', '$price', '$ip', '$quantity', '$date2')";
if ($conn->query($sql) === TRUE) {
header('Location: index.php');
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
当我点击添加到购物车时,表单提交并发布值,但只插入表格的第一行内容而不是选定的行内容。
感谢任何帮助..
答案 0 :(得分:-1)
我刚刚找到了答案。
因为有两种形式,并且它们都是嵌套的,我需要为每种产品制作唯一的形式ID。
所以只需在表单ID前面回复产品的ID,如下:
<?php
$sql3 = "SELECT * FROM cakes ORDER BY date2 DESC LIMIT 30";
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
?>
<div class="bt-item-extra product-layout odd element-4">
<section class="product-thumb bt-item transition">
<div class="image">
<a href="individual.php?id=<?php echo $row3['id'];?>"><img src="images/product/<?php echo $row3['name'];?>.jpg" style="width:200px;" alt="Tasty Cakes" title="Tasty Cakes" class="img-responsive" />
</a>
<div class="button-group button-grid">
<form action="addtocart.php" method="post" id="addtocart<?php echo $row3['id'];?>"></form>
<form action="addtowishlist.php" method="post" id="addtowishlist<?php echo $row3['id'];?>"></form>
<input type="hidden" name="ip" value="<?php echo $localIP; ?>" form="addtocart<?php echo $row3['id'];?>" />
<input type="hidden" name="cake" value="<?php echo $row3['name'];?>" form="addtocart<?php echo $row3['id'];?>" />
<input type="hidden" name="price" value="<?php echo $row3['price'];?>" form="addtocart<?php echo $row3['id'];?>" />
<input type="hidden" name="ip" value="<?php echo $localIP; ?>" form="addtowishlist<?php echo $row3['id'];?>" />
<input type="hidden" name="cake" value="<?php echo $row3['name'];?>" form="addtowishlist<?php echo $row3['id'];?>" />
<input type="hidden" name="price" value="<?php echo $row3['price'];?>" form="addtowishlist<?php echo $row3['id'];?>" />
<button class="btn-cart" type="submit" form="addtocart<?php echo $row3['id'];?>"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
<button class="btn-wishlist" type="submit" form="addtowishlist**<?php echo $row3['id'];?>**" title="Add to Wish List"><i class="fa fa-heart"></i></button>
</div>
</div>
<div class="small_detail">
<div class="caption">
<div class="name"><a href="individual.php?id=<?php echo $row3['id'];?>"><?php echo $row3['name'];?></a>
</div>
<p class="price"> ₹<?php echo $row3['price'];?>.00 </p>
</div>
</div>
</section>
</div><!-- /.bt-item-extra -->
<?php }
} else {
echo "";
}
?>
希望这有助于某人..