jeudi 18 octobre 2018

I have a shopping cart session where details are stored. How do i get those details to my database table when a checkout button is clicked

So i have this e-Commerce project I am working on. When ever an add to cart button is clicked the item is added to a shopping cart session. How do i get the details of each item in the cart to a database table when the user clicks a button. Below is a complete code of my shopping cart session, where do i add the code to get the details in the session, send them to a database table and how?

<?php
session_start();
$product_ids = array();
if(isset($_POST['addtocart'])){ 
if(isset($_SESSION['shopping_cart'])){
$count = count($_SESSION['shopping_cart']); 
$product_ids = array_column($_SESSION['shopping_cart'], 'id');
if (!in_array(filter_input(INPUT_GET, 'id'))){
$_SESSION['shopping_cart'][$count] = array
(       
'id' => filter_input(INPUT_GET, 'id'),
'name' => filter_input(INPUT_POST, 'name'),
'price' => filter_input(INPUT_POST, 'price'),
'size' => filter_input(INPUT_POST, 'size'),
'qty' => filter_input(INPUT_POST, 'qty'),
'img' => filter_input(INPUT_POST, 'img'),   
);
echo "<div style='margin: 0 80px' class='alert alert-success' 
role='alert'> Item Added to cart </div>";
}else{
echo "<div style='margin: 0 80px' class='alert alert-danger' 
role='alert'> Item Already exist in cart, scroll through our other wears 
</div>";    
header("refresh:3 index.php#item");
}   
}else{
$_SESSION['shopping_cart'][0] = array
(
'id' => filter_input(INPUT_GET, 'id'),
'name' => filter_input(INPUT_POST, 'name'),
'price' => filter_input(INPUT_POST, 'price'),
'size' => filter_input(INPUT_POST, 'size'),
'qty' => filter_input(INPUT_POST, 'qty'),
'img' => filter_input(INPUT_POST, 'img'),
);
}
}
if (filter_input(INPUT_GET, 'action') == 'delete'){
foreach($_SESSION['shopping_cart'] as $key => $r){
if ($r['id'] == filter_input(INPUT_GET, 'id')){
unset($_SESSION['shopping_cart'][$key]);
}
echo "<div style='margin: 0 80px' class='alert alert-warning' 
role='alert'>Item Removed from cart</div>";     
}
$_SESSION['shopping_cart'] = array_values($_SESSION['shopping_cart']);
}
?>




Aucun commentaire:

Enregistrer un commentaire