I manage to save the session data in db. But, only the latest value is inserted. I want all the order to be save in one row like chicken,sandwich,drink..
to get a clear picture. Here i have put 2 image showing what i mean:
a)user order's

b)database successful insert as userID 78 but only the lastest order is save in db

here are the display function code where the value is fetch:
function showOrder() {
global $conn;
$order = $_SESSION['order'];
if ($order) {
$items = explode(',',$order);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="previeworder.php?action=update" method="post" MenuID="order">';
$output[] = '<table>';
foreach ($contents as $MenuID=>$qty)
{
$result = mysql_query("SELECT * FROM menu WHERE MenuID= '".$MenuID."' ");
$row = mysql_fetch_array($result);
//extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="previeworder.php?action=delete&MenuID='.$MenuID.'" class="r">Remove</a></td>';
$name = $row['Name'];
$output[] = '<td>'.$name.'</td>';
$price = $row['Price'];
$output[] = '<td>'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$MenuID.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$subtotal = $row['Price'] * $qty;
$output[] = '<td>RM '.$subtotal.'</td>';
$total = $total + $row['Price'] * $qty;
$output[] = '</tr>';
$_SESSION['name'] = $name;
$_SESSION['price'] = $price;
$_SESSION['qty'] = $qty;
$_SESSION['subtotal'] = $subtotal;
$_SESSION['total'] = $total;
}
$output[] = '</table>';
$output[] = '<p>Grand total: RM <strong>'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update Order</button></div>';
$output[] = '</form>';
}
else
{
$output[] = '<p>You menu list is empty.</p>';
}
return join('',$output);
}
meanwhile here are the insert db code:
<?php
include("connection.php");
session_start();
$tableid = $_SESSION['q'];
$order = $_SESSION['order'];
$name = $_SESSION['name'];
$price = $_SESSION['price'];
$qty = $_SESSION['qty'];
$subtotal = $_SESSION['subtotal'];
$total = $_SESSION['total'];
mysql_query("INSERT INTO orderrecord (TableID,Menuname,Quantity,Price,Subtotal,Total) VALUES ('" . mysql_real_escape_string($tableid) . "','" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($qty) . "','" . mysql_real_escape_string($price) . "','" . mysql_real_escape_string($subtotal) . "','" . mysql_real_escape_string($total) . "') ")
or die(mysql_error());
session_destroy();
?>
plus, i would like to know why are the total undefined.
thanks in advance..🙂