I want to insert the chosen items by user from shopping cart to database after they click confirm button. The problem is, after the items inserted, the quantity value is not correct as I assume. I want the inserted quantity is determined by user in the shopping cart. But the truth is the inserted quantity is the value of quantity stock that determined by admin from the database.
Here is the code that view the shopping cart:
<table>
<tr>
<th>Peralatan Sukan</th>
<th>Kuantiti</th>
</tr>
<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM peralatansukan WHERE no IN(";
foreach((array) $_SESSION['cart'] as $id => $value){
$sql .=$id. ",";
}
$sql=substr($sql,0,-1) . ") ORDER BY no ASC";
$query = mysql_query($sql);
if(!empty($query)){
while($row = mysql_fetch_array($query)){
?>
<tr>
<td><?php echo $row['peralatansukan']; ?></td>
<td><input type="text" name="kuantiti[<?php echo $row['no']; ?>]"
size="6" value="<?php echo $_SESSION['cart'][$row['no']]['kuantiti']; ?
>"> </td>
Here is the code that update the quantity in the shopping cart :
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['kuantiti']++;
}else{
$sql_p="SELECT * FROM peralatansukan WHERE no={$id}";
$query_p=mysql_query($sql_p);
if(mysql_num_rows($query_p)!=0){
$row_p=mysql_fetch_array($query_p);
$_SESSION['cart'][$row_p['no']]=array("kuantiti" => 1);
}else{
$message="Product ID is invalid";
}
}
}
Here is the code that insert items from shopping cart to database :
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM peralatansukan WHERE no IN(";
foreach((array) $_SESSION['cart'] as $id => $value){
$sql .=$id. ",";
}
$sql=substr($sql,0,-1).") ORDER BY no ASC";
$query = mysql_query($sql);
if(!empty($query)){
while($row = mysql_fetch_array($query)){
$sql="INSERT INTO user_request(nama, noic, jawatan,
peringkat, email, no, peralatansukan, kuantiti) values
('$_SESSION[nama]', '$_SESSION[noic]',
'$_SESSION[jawatan]', '$_SESSION[peringkat]',
'$_SESSION[email]', '$row[no]', '$row[peralatansukan]',
'$row[kuantiti]')";
$res=mysql_query($sql);
unset($_SESSION['cart']);
echo '<script type="text/javascript">alert("Your request
is success.");window.location.href="viewalat.php";
</script>';
}}}
else{
echo '<script type="text/javascript">alert("Sorry.
Something went
wrong.");window.location.href="viewalat.php";
</script>';
}
I'm new to PHP. Hope you can help me out of this problem. Fyi, I'm still learning to use the mysqli. Hope you can understand. Thank you.