购物车只输入数据库中的最后一项而不是所有数据

I am having an issue with my shopping cart when I add three items only the last item enters into the database. I am not sure how to get all the items to insert into the database like 3 or 4. I have tried many different ways and still come up with nothing. I still have to also figure how to get subtotal and customer name to attach to the orders

Index.php

  <?php
    session_start();
    require_once("dbcontroller.php");
    $db_handle = new DBController();
    if(!empty($_GET["action"])) {
    switch($_GET["action"]) {
        case "add":


if(!empty($_POST["quantity"])) {
            $productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
            $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode[0]["code"] == $k) {
                                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                    $_SESSION["cart_item"][$k]["quantity"] = 0;
                                }
                                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            } else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    break;
    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;  
}
}
?>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>


<div id="product-grid">
    <div class="txt-heading">Products</div>
    <?php
    $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
    if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
    ?>
        <div class="product-item">
            <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
            <div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
            <div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
            <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
            <div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
            </form>
        </div>
    <?php
            }
    }
    ?>
</div>


<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="index.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
    $item_total = 0;
?>  
<form method="post" action="process_insert.php">
<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>Action</strong></th>
</tr>   
<?php       
    foreach ($_SESSION["cart_item"] as $item){
        ?>
                <tr>

                <td style="text-align:left;border-bottom:#F0F0F0 1px solid;" ><input type="text" name="name" value="<?php echo $item["name"]; ?>"></td>
                <td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><input type="text" name="code" value="<?php echo $item["code"]; ?>"></td>
                <td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><input type="text" name="quantity" value="<?php echo $item["quantity"]; ?>"></td>
                <td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><input type="text" name="price" value="<?php echo $item["price"]; ?>"></td>
                <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="index.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><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>

</table>        
  <?php
}
?>
<input type="submit" name="submit" value="submit">
</form>
</div>
</BODY>
</HTML>

process_insert.php

<html>
<head>
<title></title>
</head>
<body>
<?php
    ini_set('display_errors', 1);
    error_reporting(~0);

    $serverName = "localhost";
    $userName = "root";
    $userPassword = "";
    $dbName = "blog_samples";

    $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

    $sql = "INSERT INTO order_table (name, code, quantity, price) 
        VALUES ('".$_POST["name"]."','".$_POST["code"]."'
        ,'".$_POST["quantity"]."','".$_POST["price"]."')";

    $query = mysqli_query($conn,$sql);

    if($query) {
        echo "Record add successfully";
    }

    mysqli_close($conn);
?>
</body>
</html>

That's because your submit form doesnt support multiple fields. You have to add a index to each inputs' name.

<?php       
$i = 0;
foreach ($_SESSION["cart_item"] as $item){
?>
        <tr>
        <td style="text-align:left;border-bottom:#F0F0F0 1px solid;" ><input type="text" name="name[<?php echo $i; ?>]" value="<?php echo $item["name"]; ?>"></td>
        <td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><input type="text" name="code[<?php echo $i; ?>]" value="<?php echo $item["code"]; ?>"></td>
        <td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><input type="text" name="quantity[<?php echo $i; ?>]" value="<?php echo $item["quantity"]; ?>"></td>
        <td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><input type="text" name="price[<?php echo $i; ?>]" value="<?php echo $item["price"]; ?>"></td>
        <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
        </tr>
    <?php
    $item_total += ($item["price"]*$item["quantity"]);
    $i++;
}
?>

process_insert.php

<html>
<head>
<title></title>
</head>
<body>
<?php
    ini_set('display_errors', 1);
    error_reporting(~0);

    $serverName = "localhost";
    $userName = "root";
    $userPassword = "";
    $dbName = "blog_samples";

    $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

    $rows_count = count($_POST["name"]);

    for($i=0;$i<$rows_count;$i++){

        // PREVENTING SQL INJECTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        $name = mysqli_real_escape_string($conn,$_POST["name"][$i]);
        $code = mysqli_real_escape_string($conn,$_POST["code"][$i]);
        $quantity = intval($_POST["quantity"][$i]);
        $price = mysqli_real_escape_string($conn,$_POST["price"][$i]);

        $sql = "INSERT INTO order_table (name, code, quantity, price) 
            VALUES ('$name','$code','$quantity','$price')";

        $query = mysqli_query($conn,$sql);
    }

    if(mysqli_affected_rows($conn)>0) {
        echo "Record add successfully";
    }

    mysqli_close($conn);
?>
</body>
</html>