[英]PHP Shopping cart // show results of different rows
地獄社區,我正在嘗試建立一個基本上看起來像 excel 表的購物車。 布局和主要代碼運行良好,但我只能將一行中的產品添加到購物車表中。 我的目標是從我想要的行中選擇和添加盡可能多的產品。
這是我正在使用的代碼:
<?php
$query = "SELECT * FROM products ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<div id="product">
<table>
<tr>
<th>Product</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "shop");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, product, A, B, C, D, E FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$Id = $row ["id"];
$Product = $row ["product"];
$A = $row ["A"];
$B = $row ["B"];
$C = $row ["C"];
$D = $row ["D"];
$E = $row ["E"];
echo "<tr>";
echo "<td>" . $row["product"] . "</td><br />";
echo "<td>" . $row["A"] . "
<form id= \"Form_1\" method=\"post\" action=\"tbl_sc.php?action=add&id=$Id\">
<input name=\"add_to_cart\" type=\"submit\" value=\"Add to cart\"></td><br />";
echo "<td>" . $row["B"] . "
<input name=\"add_to_cart\" type=\"submit\" value=\"Add to cart\"></td><br />";
echo "<td>" . $row["C"] . "
<input name=\"add_to_cart\" type=\"submit\" value=\"Add to cart\"></td><br />";
echo "<td>" . $row["D"] . "
<input name=\"add_to_cart\" type=\"submit\" value=\"Add to cart\"></td><br />";
echo "<td>" . $row["E"] . "
<input name=\"hidden_name\" type=\"hidden\" value=\"$Product\">
<input name=\"hidden_price\" type=\"hidden\" value=\"$A\">
<input name=\"add_to_cart\" type=\"submit\" value=\"Add to cart\"></td><br />";
echo "<td>
</form></td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
?>
</table>
</div>
<?php
}
}
?>
我發現當我點擊“添加到購物車”按鈕時,我的代碼始終只顯示購物車表中“hidden_price”行中提到的變量的內容。 我必須在代碼中添加或更改什么才能將其他行(B、C、D...)添加到購物車並能夠從我想要的行中選擇產品?
預先感謝您的支持!
你想要做的不是在這個笏工作。 您應該為每個產品使用不同的表單,或者使用復選框。 下面是第一種情況的代碼。 請記住,名稱是您調用該字段的方式,而值是“您想從該字段中獲得的內容**。
當您使用 POST 時,在 action="somefile.php" 之后不需要某些內容,這就是 post 所做的,“將所有變量發送到文件 somefile.php”。 下面的唯一問題是,在“發布”數據后,您需要 tbl_sc.php 以在同一頁面中重定向。
<div id="product">
<table>
<tr>
<th>Product</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
</tr>
<?php
$Id = "111";
$Product = "xx;";
$A = 1;
$B = 2;
$C = 3;
$D = 4;
$E = 5;
<?php
$query = "SELECT * FROM products ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<div id="product">
<table>
<tr>
<th>Product</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "shop");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, product, A, B, C, D, E FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$Id = $row ["id"];
$Product = $row ["product"];
$A = $row ["A"];
$B = $row ["B"];
$C = $row ["C"];
$D = $row ["D"];
$E = $row ["E"];
echo "<tr>";
echo "<td>" . $row["product"] . "</td><br />";
echo "<form method=\"post\" action=\"tbl_sc.php\">";
echo "<td>$A <br>";
echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$A\">";
/* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */
echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";
echo "<button type=\"submit\"> Add to Cart </button>";
echo "</td>";
echo "</form>";
echo "<form method=\"post\" action=\"tbl_sc.php\">";
echo "<td>$B <br>";
echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$B\">";
/* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */
echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";
echo "<button type=\"submit\"> Add to Cart </button>";
echo "</td>";
echo "</form>";
echo "<form method=\"post\" action=\"tbl_sc.php\">";
echo "<td>$C <br>";
echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$C\">";
/* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */
echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";
echo "<button type=\"submit\"> Add to Cart </button>";
echo "</td>";
echo "</form>";
echo "<form method=\"post\" action=\"tbl_sc.php\">";
echo "<td>$D <br>";
echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$D\">";
/* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */
echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";
echo "<button type=\"submit\"> Add to Cart </button>";
echo "</td>";
echo "</form>";
echo "<form method=\"post\" action=\"tbl_sc.php\">";
echo "<td>$E <br>";
echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$E\">";
/* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */
echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";
echo "<button type=\"submit\"> Add to Cart </button>";
echo "</td>";
echo "</form>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
} else { echo "0 results"; }
?>
</table>
</div>
<?php
}
}
?>
echo "</table>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.