1 回答
TA贡献1862条经验 获得超7个赞
你正在尝试做的不是在这个 wat 上工作。您应该为每种产品使用不同的表格或使用复选框。下面是第一种情况的代码。请记住,名称是您如何称呼该字段,而值是“您想从该字段中获得什么**。
当你使用 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>";
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报