亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號