Im trying to save my cart item into database. This is what i tried and it didnt work, can anyone please help me.
if (isset($_POST['submit']))
{
include ('config.php');
foreach ($_SESSION["products"] as $cart_itm)
{
$kod_barang = $cart_itm["kod_barang"];
$nama_barang = $cart_itm["nama_barang"];
$kuantiti = $cart_itm["qty"];
$insert_row = $mysqli->query("INSERT INTO `maklumat_permohonan`(`kod_barang`, `nama_barang`, `kuantiti`, `nombor_order`)
VALUES ('$kod_barang','$nama_barang','$kuantiti')");
if($insert_row){
print 'Please keep the Transaction ID for future reference<br />';
?>
<a href="index.php">Back</a>
<?php
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
}
}
This is how my cart session look like, im sorry if my code is not too decent and proper, as this is my early learning in PHP using session for my thesis in Server client topics.
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<form method="post" action="simpan_permohonan.php">';
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm)
{
$product_code = $cart_itm["code"];
$results = $mysqli->query("SELECT nama_barang,jenis_barang FROM data_barang WHERE kod_barang='$product_code' LIMIT 1");
$obj = $results->fetch_object();
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'"> ×</a></span>';
echo '<div class="product-info">';
echo '<h3>'.$obj->nama_barang.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty">Kuantiti : '.$cart_itm["qty"].'</div>';
echo '<div>'.$obj->jenis_barang.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->nama_barang.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->jenis_barang.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
$cart_items ++;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<input type="submit" value="Simpan" />';
echo '</span>';
echo '</form>';
}else{
echo 'Troli Anda Kosong';
}