PHP 购物车 // 显示不同行的结果

地狱社区,我正在尝试建立一个基本上看起来像 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"];


                       

                    }

                   

我发现,当我单击“添加到购物车”按钮时,我的代码始终只显示“hidden_price”行中提到的变量的购物车表中的内容。我必须在代码中添加或更改什么才能将其他行(B、C、D...)添加到购物车并能够从我想要的行中选择产品?


预先感谢您的支持!


慕桂英4014372
浏览 112回答 1
1回答

牧羊人nacy

你正在尝试做的不是在这个 wat 上工作。您应该为每种产品使用不同的表格或使用复选框。下面是第一种情况的代码。请记住,名称是您如何称呼该字段,而值是“您想从该字段中获得什么**。当你使用 POST 时,你不需要在 action="somefile.php" 之后做任何事情,这就是 post 所做的,“将所有变量发送到文件 somefile.php”。以下唯一的问题是您在“发布”数据后需要您的 tbl_sc.php 以在同一页面中重定向。<div id="product">&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Product</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>A</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>B</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>C</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>D</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>E</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Id = "111";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Product = "xx;";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $A = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $B = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $C = 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $D = 4;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $E = 5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; $query = "SELECT * FROM products ORDER BY id ASC";&nbsp; &nbsp; $result = mysqli_query($connect, $query);&nbsp; &nbsp; &nbsp; &nbsp; if(mysqli_num_rows($result) > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while($row = mysqli_fetch_array($result))&nbsp; &nbsp; {?><div id="product">&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Product</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>A</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>B</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>C</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>D</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>E</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $conn = mysqli_connect("localhost", "root", "", "shop");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check connection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($conn->connect_error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die("Connection failed: " . $conn->connect_error);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = "SELECT id, product, A, B, C, D, E FROM products";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Id = $row ["id"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Product = $row ["product"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $A = $row ["A"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $B = $row ["B"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $C = $row ["C"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $D = $row ["D"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $E = $row ["E"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["product"] . "</td><br />";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<form method=\"post\" action=\"tbl_sc.php\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>$A <br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$A\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<button type=\"submit\"> Add to Cart </button>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</form>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<form method=\"post\" action=\"tbl_sc.php\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>$B <br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$B\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<button type=\"submit\"> Add to Cart </button>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</form>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<form method=\"post\" action=\"tbl_sc.php\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>$C <br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$C\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<button type=\"submit\"> Add to Cart </button>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</form>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<form method=\"post\" action=\"tbl_sc.php\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>$D <br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$D\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<button type=\"submit\"> Add to Cart </button>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</form>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<form method=\"post\" action=\"tbl_sc.php\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>$E <br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"add_to_cart\" type=\"hidden\" value=\"$E\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id\ in action */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<button type=\"submit\"> Add to Cart </button>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</form>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</table>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { echo "0 results"; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </table></div><?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</table>";
打开App,查看更多内容
随时随地看视频慕课网APP