gpt4 book ai didi

php - 提交按钮内有多个 ID 循环?(购物车)

转载 作者:行者123 更新时间:2023-11-29 13:07:11 24 4
gpt4 key购买 nike

我想在 PHP 上创建购物车,

代码很简单,当客户填写数量并单击“添加到购物车”按钮时,代码会将产品 ID 和数量保存到购物车表中。但问题是Form里面的Looping。以及如何仅从客户点击的按钮获取 ID 和数量。

程序看起来像这样

enter image description here

像这样的脚本

<?php

if(isset($_POST[ADD]))
{
$qty = $_POST[QTY];
$harga = $_POST[HARGA_ASLI];
$id = $_POST[ID];

print_r($_POST);
}

$kolom = 3;

$sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";

$hasil = mysql_query($sql);
echo "<form method=POST action=index.php>";
echo "<table>
<tr>";
$i = 0;
while($data=mysql_fetch_array($hasil))
{
if($i >= $kolom)
{
echo "</tr><tr>";
$i = 0;
}
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
Rp. $data[harga_digit]<br>
<input type='submit' name='ADD' id='ADD' value='Add to Cart'>
<input type='text' name='QTY' id='QTY' placeholder='Qty' /><br>
<input type='hidden' name='HARGA_ASLI' id='HARGA_ASLI' value='$data[harga]' /><br>
<input type='hidden' name='ID' id='ID' value='$data[ID]' />
<br></td>";


}//end of while

echo "<tr></table>";
echo "</form>";

?>

如果我填写数量并单击“添加到购物车”,则只有最后一个项目可以发布数据。

如何发布仅供客户选择的数据?

我非常感谢您的回答。

谢谢

最佳答案

首先,让我们将 MySQL 转换为 MySQLi。更多解释见评论/* */:

<?php   
$connection=mysqli_connect("YourHost","YourUsername","YourPassword","NameofYourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();

}

$res=mysqli_query($con,"SELECT * FROM item");

while($row=mysqli_fetch_array($res)){

$nameofsubmitbutton=$row['ID'];

if(isset($_POST[$nameofsubmitbutton])){

$nameofproduct=$row['namaproduk'];
$nameofnumbersubmitted=$nameofsubmitbutton."number";
$quantity=$_POST[$nameofnumbersubmitted];

if(empty($quantity)){
echo "You wanted to buy a ".$nameofproduct."?<br>Type in a number so you can add it to your cart.";
}

else {
mysqli_query($connection,"INSERT INTO yourTable ('','') VALUES ('$quantity','$nameofproduct')");
echo "You bought ".$quantity." of ".$nameofproduct;
}

} /* END OF IF ISSET */

} /* END OF WHILE LOOP $RES */

$kolom = 3;

$hasil = mysqli_query($connection,"SELECT *,FORMAT(harga,0) AS harga_digit FROM item"); /* YOU SURE WITH THIS QUERY? */

echo "<form method=POST action=''>"; /* SUBMIT ON ITSELF */
echo "<table><tr>";

$i = 0; /* THIS WOULD ALSO SET AS YOUR COUNTER */
while($data=mysqli_fetch_array($hasil))
{

$id=$data['ID'];

if($i >= $kolom){

echo "</tr><tr>";
$i = 0;

} /* END OF IF $i >= $KOLOM */

$i++;

echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>".$data[nama_produk]."</a><br>Rp. ".$data[harga_digit]."<br>"; /* IF TO ECHO VARIABLES, USE ".$variable." */

$numbername=$id."number";

echo "<input type='number' name='$numbername' id='QTY' placeholder='Qty' /><br>"; /* CHANGE YOUR INPUT TYPE TO NUMBER */

/* NO NEED FOR THE HIDDEN INPUT */

echo "<input type='submit' name='$id' id='ADD' value='Add to Cart'></td>"; /* CHANGE THE NAME OF SUBMIT BUTTON TO THE CORRESPONDING ID FROM YOUR TABLE */



} /* END OF WHILE LOOP */

echo "<tr></table>";
echo "</form>";

?>

我在本地计算机上尝试过。你也应该这样做。

这是一个示例屏幕截图。 enter image description here

关于php - 提交按钮内有多个 ID 循环?(购物车),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22549380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com