我在加入購物車時遇到了一些麻煩。我需要購物車能夠添加物品數量的總和,然後在訂單中添加總計數量,然後最後單擊一個地方訂單按鈕,以便能夠訂購購物車內的所有物品。如何在購物車中添加總量並在點擊購物車的地方下單?
目前我可以更改數量,但必須選擇產品編號,然後選擇一個數量,而不是在購物車的每個商品附近應該有一個小框,以便我可以輸入數量。
然後,我需要在底部添加所有項目的總計總數和點擊時的訂單按鈕按鈕應該一次訂購購物車中的所有物品。
目前我必須點擊每個項目的下訂單才能下訂單,所以我必須前後一步,這不是正確的做法。
請幫助我,我仍然是一個學習者。
在此先感謝 我將提供代碼。
<?php
//Start session
session_start();
$totalAll = 0;
//Include session details
require_once('auth.php');
//Include database connection details
require_once('connection/config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//checks if id is set in the url
if(isset($_GET['id'])){
//retrive the first quantity from the quantities table
$quantities=mysql_query("SELECT * FROM quantities")
or die("Something is wrong ... \n" . mysql_error());
$row=mysql_fetch_assoc($quantities);
$quantity_value = $row['quantity_value'];
//get id value
$food_id = $_GET['id'];
//retrive food_price from food_details based on $food_id
$result=mysql_query("SELECT * FROM food_details WHERE food_id='$food_id'") or die("A problem has occured ... \n" . "Our team is working on it at the moment ... \n" . "Please check back after few hours.");
$food_row=mysql_fetch_assoc($result);
$food_price=$food_row['food_price'];
//get member_id from session
$member_id = $_SESSION['SESS_MEMBER_ID'];
//define default values for quantity(got from $row), total($food_price*$quantity_value), and flag_0
$quantity_id = $row['quantity_id'];
$total = $food_price*$quantity_value;
$flag_0 = 0;
//Create INSERT query
$qry = "INSERT INTO cart_details(member_id, food_id, quantity_id, total, flag) VALUES('$member_id','$food_id','$quantity_id','$total','$flag_0')";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
header("location: cart.php");
exit();
}else {
die("A problem has occured with the system " . mysql_error());
}
}
?>
我不能完全確定你想要什麼,但您的第一句話我會猜測您需要在代碼中總結數量和價格的循環。在你的代碼示例中沒有這樣的循環。這樣的循環看起來像這樣:while($ row = mysql_fetch_array($ result)){/ *添加一個變量* /} – CodingYourLife 2014-11-20 20:46:35
我的意思是例如:£2.50然後選擇一個選項,如2這將使該物品的總價值爲5.00英鎊,然後我需要購物車中所有物品的總計 – WebProgrammer 2014-11-20 20:59:58