我在购物车中添加总额时遇到了一些麻烦。我需要购物车能够添加物品总数,然后在订单中添加总计,然后最后单击“下订单”按钮,以便能够在购物车内订购所有物品。
目前我可以更改数量,但我必须选择产品ID,然后选择一个数量,而不是这个,在购物车中的每个项目附近应该有一个小盒子,我可以输入数量。
然后我需要在底部添加所有项目的总计,并且在点击购物车中的所有商品时,可以一次性订购“下订单”按钮。
目前我必须点击每个项目的下订单才能下订单,所以我必须前后退,这不是正确的做法。
请帮助我,我仍然是学习者。
提前致谢 我会提供代码。
<?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());
}
}
?>
答案 0 :(得分:0)
您的代码目前只能从网址获取一个ID,但为了计算整个购物车的总数,您需要购物车中的所有ID。通常他们会被保存到$ _SESSION。您可以使用以下代码查看会话内部:var_dump($ _ SESSION);也可能将ID保存到数据库中某种类型的购物车表。在您的代码中,我不明白为什么您只是从数量表中获取第一个项目。我很确定桌子以某种方式连接到食物桌上(我想通过food_id)。
如果您从会话中获取了ID,那么您的代码看起来应该是这样的(因为sql语句而不是testet):
<?php
$member_id = $_SESSION['SESS_MEMBER_ID']; //get member_id from session before the loop
$food_ids = array($id1, $id2, $id3); //you need all $food_id added to cart instead of just 1 from url, usally saved to $_SESSION
$grandTotal = 0;
foreach ($food_ids as $food_id) { //LOOP THROUGH IDs
//retrive quantity of food in cart from the quantities table
$quantities=mysql_query("SELECT * FROM quantities WHERE food_id='$food_id'") //there must be a connection between quantities and food so I added food_id="xxx"
or die("Something is wrong ... \n" . mysql_error());
$row=mysql_fetch_assoc($quantities);
$quantity_value = $row['quantity_value'];
//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'];
//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;
$grandTotal = $grandTotal + $total; //add total of this product to the $grandTotal from last time inside this loop
$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());
}
} /*Now you have $grandTotal calculated*/?>