为什么foreach在最后一行复制?

时间:2017-11-23 15:38:56

标签: php

$_SESSION["cart_item"]有4行,但在数据库中,最后一行有4 + 1个重复

foreach($_SESSION["cart_item"] as $item1){
    $sqlinsert="INSERT INTO `t_rinci_jual_tunai` (`kd_operasional`, `kd_barang`, `jumlah`, `harga_satuan`, `kd_satuan`) VALUES ('2', '" . $item1["kd_barang"] . "', '" . $item1["quantity"] . "', '" . $item1["harga_patokan"] . "', '505')";
    $insert= mysqli_query($conn, $sqlinsert);
}

Screenshot

<head>
<link rel="stylesheet" type="text/css" href="css/Style.css">
</head>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;"><strong>Name</strong></th>
<th style="text-align:left;"><strong>Code</strong></th>
<th style="text-align:right;"><strong>Quantity</strong></th>
<th style="text-align:right;"><strong>Price</strong></th>
<th style="text-align:center;"><strong>Total</strong></th>
<th style="text-align:center;"><strong>Action</strong></th>
</tr>

<?php
session_start();
require_once("Connecting.php");
 if (isset($_GET['enter']) && ($_SESSION["cart_item"])){    
      echo "enter ada"; 
            switch ($_GET["enter"]){
                case "enter";



                        foreach($_SESSION["cart_item"] as $item1){



                        $sqlinsert="INSERT INTO `t_rinci_jual_tunai` (`kd_operasional`, `kd_barang`, `jumlah`, `harga_satuan`, `kd_satuan`) VALUES ('2', '" . $item1["kd_barang"] . "', '" . $item1["quantity"] . "', '" . $item1["harga_patokan"] . "', '505')";
                    $insert= mysqli_query($conn, $sqlinsert);

                ?>  <tr>
                <td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><strong><?php echo $item1['name']; ?></strong></td>
                <td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><?php echo $item1["kd_barang"]; ?></td>
                <td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><?php echo $item1["quantity"]; ?></td>
                <td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><?php echo "$".$item1["harga_patokan"]; ?></td>

                </tr>
                <?php   

                    }

                    if (mysqli_query($conn, $sqlinsert)) {
    echo "New record created successfully";

} else {
    echo "Error: " . $sqlinsert . "<br>" . mysqli_error($conn);
}       
            break; } 
            } else {
                echo "enter tidak ada ";

            }

?>

</tbody>
</table>

1 个答案:

答案 0 :(得分:0)

你应该使用foreach而不是:

foreach ($_SESSION["cart_item"] as $item1) { ... }