 |
|
ปัญหาจาก code ด้านล่างมี 2 ปัญหาคือ
1. บันทึกลง orderlist แค่รายการเดียว แต่มี record ว่างเพิ่มมาอีก 1 record ค่ะ
2. รายการเก็บลงตระกร้า 2 รายการ แต่ลงตาราง order_detail แค่รายการเดียวและเป็นรายการสุดท้ายเพียง 1 รายการค่ะ
ต้องแก้ยังไงคะ
Code (PHP)
<?
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
if (isset($_POST['orderid']) && isset($_POST['productid']) && isset($_POST['qty']) && isset($_POST['sum'])){
$ITEM = array(
'name' => $_POST['product_name'],
'order_id' => $_POST['orderid'],
'product_id' => $_POST['productid'],
'qty' => $_POST['qty'],
'price' => $_POST['sum'],
);
$_SESSION['SHOPPING_CART'][] = $ITEM;
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['remove'])){
unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['empty'])){
session_destroy();
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_POST['update'])) {
foreach ($_POST['items_qty'] as $itemID => $qty) {
if ($qty == 0) {
unset($_SESSION['SHOPPING_CART'][$itemID]);
}
else if($qty >= 1) {
$_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
}
}
//Clear the POST variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
$host="localhost";
$username="root";
$password="123456";
$db="shopcart";
$tb="orderlist";
$connect= mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql="select * from $tb";
$db_query=mysql_db_query($db,$sql);
$date_now = date("Y-m-d");
mysql_query("INSERT INTO $tb (id, order_id,order_name,order_email,order_phone,order_address,order_total,order_date) values('', '$_POST[orderid]','$_POST[order_name]','$_POST[order_email]','$_POST[order_phone]','$_POST[order_address]','$_POST[total_order]','$date_now')") or die ("Add ข้อมูลลง Table ไม่ได้ ");
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
$insert2 = "insert into order_detail (id,order_id,product_id,order_number,order_price) VALUES ('','$item[order_id]','$item[product_id]','$item[qty]','$item[price]')";
$result2 = mysql_query($insert2);
}
?>
ok sure
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-01-14 16:37:20 |
By :
tukata |
View :
922 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |