为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕桂英4014372 2022-12-23 15:50:23
地狱社区,我正在尝试建立一个基本上看起来像 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...)添加到购物车并能够从我想要的行中选择产品?预先感谢您的支持!
查看完整描述

1 回答

?
牧羊人nacy

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>";



查看完整回答
反对 回复 2022-12-23
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信