将会话数组中的购物车项添加到mysql数据库中

How can send the data from shopping cart into mysql database, at the moment i can add items into shopping cart and can clear however i have created checkout button as well but cant figure out on how to get the data from shopping cart to mysql database, i have tried the action check_out but am i doing this totaly wrong

this is my action.php which gets the items in shopping cart to array and adds or removes them.

<?php
include('cart/database_connection.php');
//action.php

session_start();

if(isset($_POST["action"]))
{
    if($_POST["action"] == "add")
    {
        if(isset($_SESSION["shopping_cart"]))
        {
            $is_available = 0;
            foreach($_SESSION["shopping_cart"] as $keys => $values)
            {
                if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
                {
                    $is_available++;
                    $_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
                }
            }
            if($is_available == 0)
            {
                $item_array = array(
                    'product_id'               =>     $_POST["product_id"],  
                    'product_name'             =>     $_POST["product_name"],  
                    'product_price'            =>     $_POST["product_price"],  
                    'product_quantity'         =>     $_POST["product_quantity"]
                );
                $_SESSION["shopping_cart"][] = $item_array;
            }
        }
        else
        {
            $item_array = array(
                'product_id'               =>     $_POST["product_id"],  
                'product_name'             =>     $_POST["product_name"],  
                'product_price'            =>     $_POST["product_price"],  
                'product_quantity'         =>     $_POST["product_quantity"]
            );
            $_SESSION["shopping_cart"][] = $item_array;
        }
    }

    if($_POST["action"] == 'remove')
    {
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["product_id"] == $_POST["product_id"])
            {
                unset($_SESSION["shopping_cart"][$keys]);
            }
        }
    }
    if($_POST["action"] == 'empty')
    {
        unset($_SESSION["shopping_cart"]);
    }


    if($_POST["action"] == 'check_out')
    {
        if(isset($_SESSION["shopping_cart"]))
        {
            foreach($_SESSION["shopping_cart"] as $values)
        {
             $sql ="INSERT INTO orders (total, product_id)
                    values ('{$values['product_id']}','{$v['total']}')";
                    $statement = $connect->prepare($query);
                    $statement->execute();
        if ($statement) {
            $_SESSION['success'] = 'Information updated successfully';

            header("location: my_account.php");
            exit;
        } else {
            $_SESSION['errormsg'] = 'Someting is wrong in updating your Information, Please try again later.';
            header("location: my_account2.php");
            exit;
        }}
    }

}
?>

and this is the script i am using the call the functions in my index.php

<script>  
$(document).ready(function(){
    load_cart_data();

    function load_product()
    {
        $.ajax({
            url:"cart/fetch_item.php",
            method:"POST",
            success:function(data)
            {
                $('#display_item').html(data);
            }
        });
    }

    function load_cart_data()
    {
        $.ajax({
            url:"cart/fetch_cart.php",
            method:"POST",
            dataType:"json",
            success:function(data)
            {
                $('#cart_details').html(data.cart_details);
                $('.total_price').text(data.total_price);
                $('.badge').text(data.total_item);
            }
        });
    }

    $('#cart-popover').popover({
        html : true,
        container: 'body',
        content:function(){
            return $('#popover_content_wrapper').html();
        }
    });

    $(document).on('click', '.add_to_cart', function(){
        var product_id = $(this).attr("id");
        var product_name = $('#name'+product_id+'').val();
        var product_price = $('#price'+product_id+'').val();
        var product_quantity = $('#quantity'+product_id).val();
        var action = "add";
        if(product_quantity > 0)
        {
            $.ajax({
                url:"cart/action.php",
                method:"POST",
                data:{product_id:product_id, product_name:product_name, product_price:product_price, product_quantity:product_quantity, action:action},
                success:function(data)
                {
                    load_cart_data();
                    alert("Item has been Added into Cart");
                }
            });
        }
        else
        {
            alert("Please Enter Number of Quantity");
        }
    });

    $(document).on('click', '.delete', function(){
        var product_id = $(this).attr("id");
        var action = 'remove';
        if(confirm("Are you sure you want to remove this product?"))
        {
            $.ajax({
                url:"cart/action.php",
                method:"POST",
                data:{product_id:product_id, action:action},
                success:function()
                {
                    load_cart_data();
                    $('#cart-popover').popover('hide');
                    alert("Item has been removed from Cart");
                }
            })
        }
        else
        {
            return false;
        }
    });

    $(document).on('click', '#clear_cart', function(){
        var action = 'empty';
        $.ajax({
            url:"cart/action.php",
            method:"POST",
            data:{action:action},
            success:function()
            {
                load_cart_data();
                $('#cart-popover').popover('hide');
                alert("Your Cart has been clear");
            }
        });
    });

$(document).on('click', '#check_out_cart', function(){
        var action = 'empty';
        $.ajax({
            url:"cart/action.php",
            method:"POST",
            data:{action:action},
            success:function()
            {
                load_cart_data();
                $('#cart-popover').popover('hide');
                alert("Your Cart has been clear");
            }
        });
    });




});

</script>

databaseconnection.php

<?php

//database_connection.php

$connect = new PDO("mysql:host=localhost;dbname=foodsystem", "root", "");

?>

i have check the cart session and i can see the items in the array

Array
(
    [0] => Array
        (
            [product_id] => 1
            [product_name] => Chicken Burger
            [product_price] => 10
            [product_quantity] => 1
        )

    [1] => Array
        (
            [product_id] => 2
            [product_name] => Fish Burger
            [product_price] => 10
            [product_quantity] => 1
        )

    [2] => Array
        (
            [product_id] => 3
            [product_name] => Ham Burger
            [product_price] => 10
            [product_quantity] => 1
        )

You define a variable called $sql and you run one called $query.

These kinds of mistakes happen because queries as variables first and executing second is fundamentally problematic. It's much better to define the query as an argument so you can't "miss":

$stmt = $connect->prepare("INSERT INTO orders (total, product_id) VALUES (:total, :product_id)");
$stmt->execute($v);

There is zero chance that runs the wrong query, or no query at all, by accident.

This also fixes the SQL injection bug by using placeholder values, something that's so ridiculously easy with PDO that there's really no excuse to not do it. If you match up the named placeholders in the query with the keys in $v it will run with the right data automatically.