dstobkpm908182 2019-02-14 10:30
浏览 43

如何将每行记录保存到数据库中?

How to insert each line of record into database's table "receipt"?

enter image description here

Each row of purchase record as the picture.

This are the recent code that I implemented

if (isset($_POST["add"])){
    if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
        $_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
    }
    $_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
    $_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
    $_SESSION["cart"][$_GET["id"]]['item_quantity'] = $_SESSION["cart"][$_GET["id"]]['item_quantity'] + $_POST["quantity"];

    header('Location: counter.php');
}

if (isset($_GET["action"])){
    if ($_GET["action"] == "delete"){
        foreach ($_SESSION["cart"] as $keys => $value){
            if ($value["id"] == $_GET["id"]){
                unset($_SESSION["cart"][$keys]);
                echo '<script>alert("Product has been Removed")</script>';
                echo '<script>window.location="counter.php"</script>';
            }
        }
    }
}

if (isset($_POST["minus"])){
    if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
        $_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
    }
    $_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
    $_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
    $_SESSION["cart"][$_GET["id"]]['item_quantity'] = ( ( ( $_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] ) > 1 ) ? 
    $_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] : 1 );

    header('Location: counter.php');
}
<?php 
    $total_price = 0;
    $change = 0;
    $selectOption = 0; 
    if(!empty($_SESSION["cart"])){
    foreach ($_SESSION["cart"] as $key => $value) {
?>  

            <tr>
                <td style="background-color:#FFB6C1"; width="7%"><?php echo $value["item_name"]; ?></td>
                <td style="text-align:center;background-color:#E0FFFF"; width="1%"><?php echo $value["item_quantity"]; ?></td>
                <td style="text-align:center;background-color:#D8BFD8"; width="3%">RM <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td>
                <td style="text-align:center;background-color:#FAFAD2"; width="3%"><a href="counter.php?action=delete&id=<?php echo $value["id"]; ?>">
                    <img src="img/icon-delete.png"/></a></td>
            </tr>

<?php 
    $total_price = $total_price + ($value["item_quantity"] * $value["product_price"]); 
?>

<!--for onkeyup-->
<script>      
var total = '<?php echo $total_price ;?>';
</script>

<?php
    if(isset($_POST["cash"])){
        $cash = $_POST['cash'];
        $change = $_POST["cash"] - $total_price;
        $_SESSION["cash"] = $_POST["cash"];
    }
    else {
        $change = "0";
        $_SESSION["cash"] = 0;
    }
}
}

?>
            </table>
            </div>

            <div style="border:0px solid black; width:400px">
            <table class="table table-bordered table-striped" width="200px">
            <form method="POST" action="receipt.php">
            <tr>
            <th colspan="2" style="text-align:right">Total:</th>
            <th style="text-align:center;color:white;background-color:#9932CC" id="total">RM <?php echo number_format($total_price,2); $_SESSION["total"] = $total_price;?></th>

            </tr>

            <tr>
            <th colspan="2" style="text-align:right">Cash:</th>
            <th><center>
            <input type="number" id="myInput" style="width:90px;padding:0;" onkeyup="myFunction()" name="cash" step=".01">
            </center>
            </th>
            </tr>

            <th colspan="2" style="text-align:right;">Change:</th>
            <th style="text-align:center;width:90px;color:white;background-color:#00FF00" id="change">RM <?php echo number_format($change,2); ?></th>
            </tr>

            </table>

            </div>  

            <div style="border:0px solid black; width:400px">
            <table class="table table-bordered table-striped" style="margin-top:-30px">
        <tr>
        <th style="text-align:center">

        <input type="hidden" name="action" value="submit"></input></td>
        <input type="submit" name="payment" value="Payment" id="submit_purchase" class="btn btn-warning btn-xs">
        </form>
        </th></tr>
        </table>

        </div></div>
     </div>
        <script>
        function myFunction() {
            var x = document.getElementById("myInput").value;
            var y = total;
            var z = x - y;
            var z = z.toFixed(2);
            document.getElementById("change").innerHTML = z;
        }
        </script>  

//receipt.php
session_start();
if(isset($_POST["action"]))
{
if($_POST["action"] == 'submit')
{

    if(isset($_SESSION["cart"]))
    {
        $link=mysqli_connect("localhost","root","","testing");  
        foreach($_SESSION["cart"] as $keys => $values)
        {
            $name    =$values["product_name"];
            $quantity=$values["product_quantity"];
            $price   =$values["product_price"];
            $subprice=$values["product_quantity"] * $values["product_price"];

            date_default_timezone_set("Asia/Kuala_Lumpur");
            $date = date("Y-m-d");

        $total_price = $total_price + ($values["product_quantity"] * $values["product_price"]);
        $total_item = $total_item + 1;

        $query="INSERT INTO receipt(purchase_name, product_quantity, price, subprice, total, date)
        VALUES('$name','$quantity','$price','$subprice', '$total_price', '$date')";

        if(mysqli_query($link,$query)){
            print "<p>The entry has been added!</p>"; 
        }else{
            print '<p style="color:red;">Could not add the entry because:<br>' . mysqli_error($link) .
            '.</p><p>The query being run was: ' . $query .' </p>';
        }

    }
    mysqli_close($link);}}}

    if(isset($_SESSION["cart"])){   
    unset($_SESSION['cart']);
    }
?>

Store all the transaction after clicking the payment into the receipt table, the attribute such as

receipt

  • receipt_id
  • purchase_name
  • product_quantity
  • price
  • subprice
  • total
  • date
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了