如何将php cart数据插入mysql数据库

时间:2017-09-28 05:14:53

标签: php mysql

enter image description here我用PHP开发了购物车,但是在将购物车数据插入数据库时​​,我得到了一个未定义索引的错误。请问有谁请告诉我如何解决?我已经使用POST方法调用了所有这三个值,这些都存在于数据库中,但我仍然收到此错误。为什么? 代码:

reviewcart.php
    <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "midata";
     // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
    } 
    if(isset($_POST['submit']))
         {

            $name=$_POST['name'];
            $code=$_POST['code'];
            $quantity=$_POST['quantity'];
            $sql = "INSERT INTO order_final (name, code, quantity)
            VALUES ('$name', '$code', '$quantity')";
            if ($conn->query($sql) === TRUE) {
                }
                    else{                                                    
                        }
       }
        else{   echo 'query doesnt execute';
         }
             ?>

    <div class="container">
        <?php
    session_start();
    require_once("dbcontroller.php");
    $db_handle = new DBController();
    if(!empty($_GET["action"])) {
    switch($_GET["action"]) {

        case "remove":
            if(!empty($_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($_GET["code"] == $k)
                            unset($_SESSION["cart_item"][$k]);              
                        if(empty($_SESSION["cart_item"]))
                            unset($_SESSION["cart_item"]);
                }
            }
        break;
        case "empty":
            unset($_SESSION["cart_item"]);
        break;  
    }
    }
    ?>

        <div class="c-layout-sidebar-content ">
            <!-- BEGIN: PAGE CONTENT -->
            <div class="c-content-title-1 c-margin-t-30">
                <h3 class="c-font-uppercase c-font-bold c-center ">Place Order</h3>

                <div class="c-content-panel">

                    <div class="c-body">
                        <div class="row">
                            <div class="col-md-12">
                                <table class="table table-condensed">
                                   <div id="shopping-cart">
                                     <div class="txt-heading">Shopping Cart <a id="btnEmpty" href="reviewcart.php?action=empty">Empty Cart</a></div>
                                     <?php
                                         if(isset($_SESSION["cart_item"])){
                                            $item_total = 0;
                                        ?>  
                                    <table class="col-md-12" cellpadding="10" cellspacing="1" >
                                        <tbody class="col-md-12">
                                            <tr>
                                                 <form action="" method="post">
                                                <th class="col-md-3" style="text-align:center;"><strong>Name</strong></th>
                                                <th class="col-md-3" style="text-align:center;"><strong>Code</strong></th>
                                                <th class="col-md-3" style="text-align:center;"><strong>Quantity</strong></th>
                                                <th class="col-md-3" style="text-align:center;"><strong>Price</strong></th>
                                                <th class="col-md-3" style="text-align:center;"><strong>Action</strong></th>
                                            </tr>   
                                            <?php       
                                                foreach ($_SESSION["cart_item"] as $item){
                                            ?>
                                            <tr>

                                                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><strong><?php  $name=$_POST['name'] ;  echo  $item["name"]; ?></strong></td>
                                                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $code=$_POST['code'] ; echo  $item["code"]; ?></td>
                                                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $quantity=$_POST['quantity'] ;  echo  $item["quantity"]; ?></td>
                                                    <!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php  $price=$_POST['price'] ; echo $price ?></td> -->
                                                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="reviewcart.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
                                            </tr>

                                            <!-- <?php
                                                $item_total += ($item["price"]*$item["quantity"]);
                                                }
                                            ?>
     -->
                                            <tr>
                                                <td colspan="5" align=right><input type="submit" name="submit" value="submit" /></td>
                                            </tr></form>

                                        </tbody>
                                    </table>        
                                    <?php
                                        }
                                    ?>
                                    </div>

                                </table>
                            </div>
                        </div>

                    </div>
                </div>

            </div>
        </div>

    </div>

1 个答案:

答案 0 :(得分:1)

如果我们想通过表单发布任何数据意味着我们必须要使用输入标记,那么我们只能接受全局数组上的值,如$ _POST,$ _GET,$ _FILES。

                 <tr>
                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                        <strong><?php echo  $item["name"]; ?></strong>
                        <input type="hidden" name="name" value="<?php echo $item['name']?>">
                    </td>
                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                        <?php echo  $item["code"]; ?>
                        <input type="hidden" name="code" value="<?php echo $item['code']?>">
                    </td>
                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                        <?php echo  $item["quantity"]; ?>
                        <input type="hidden" name="quantity" value="<?php echo $item['quantity']?>">
                    </td>
                    <!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                        <?php  $price=$_POST['price'] ; echo $price ?>
                        </td> -->
                    <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                        <a href="reviewcart.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">
                           Remove Item
                        </a>
                    </td>
                </tr>