我正在用 php/mysql 编写一个程序,其中登录的用户可以通过我存储在会话中的购物车订购商品。我想要实现的是从会话中获取此购物车信息,并在用户提交订单时将其放入我的数据库中。我的系统中不涉及付款信息。我的 php 知识是基础到中低级。
我的购物车页面显示了用户购物车中的商品,通过以下方式进行。
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table width=\"100%\" border=\"0\" padding=\"3\" align=\"center\">"; //format the cart using a HTML table
echo "<tr>";
echo "<th align=\"center\"></th>";
echo "<th align=\"center\">Quantity</th>";
echo "<th align=\"center\">Item</th>";
echo "<th align=\"center\">Price</th>";
echo "</tr>";
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $item_id => $quantity) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT title, price FROM food WHERE food_id = %d;", $item_id);
$result = mysql_query($sql);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($title, $price) = mysql_fetch_row($result);
$line_cost = number_format($price * $quantity, 2); //work out the line cost
$total = number_format($total + $line_cost, 2); //add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&id=$item_id\" data-role=\"button\" data-icon=\"minus\" data-iconpos=\"notext\" data-transition=\"none\">Remove Item</a></td>";
echo "<td align=\"center\">$quantity</td>";
echo "<td>$title</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$$line_cost</td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<th colspan=\"3\" align=\"right\" >Total</th>";
echo "<td align=\"center\">$$total</td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"4\" align=\"right\"><a href=\"/delete_cart.php\" data-role=\"button\" data-icon=\"delete\" data-iconpos=\"left\" data-rel=\"dialog\">Empty Cart</a></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"4\" align=\"center\"><a href=\"/order.php\" data-role=\"button\">Order</a></td>";
echo "</tr>";
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "<p align=\"center\">You have no items in your shopping cart.</p>";
}
然后在订单页面上,我再次检索购物车信息并从用户那里获得更多详细信息。下面是将订单信息提交到我的订单表的代码。
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "submit_order")) {
$insertSQL = sprintf("INSERT INTO orders (user_id, `date`, total, info) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['user_id'], "int"),
GetSQLValueString($_POST['date'], "text"),
GetSQLValueString($_POST['total'], "text"),
GetSQLValueString($_POST['info'], "text"));
mysql_select_db($database_ngs_canteen, $ngs_canteen);
$Result1 = mysql_query($insertSQL, $ngs_canteen) or die(mysql_error());
$insertGoTo = "/ngs_canteen/submit.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
我在将购物车会话中的项目输入数据库时遇到问题。我在此页面上插入用户 ID、总价、订单日期的其他表单工作正常,但我无法终生弄清楚如何提交购物车项目。
我已经尝试过这样的事情,但到目前为止还没有成功。
$result = @mysql_query($query);
if (@mysql_affected_rows($dbc) == 1) {
// Get the Order ID.
$oid = @mysql_insert_id($dbc);
// Insert the specific order contents into the database.
$query = "INSERT INTO order_items (order_id, food_id, quantity, price) VALUES (";foreach ($_SESSION['cart'] as $item_id => $value) {
$query .= "$oid, $food_id, {$value['quantity']}, {$value['price']})";
}
任何帮助将不胜感激,即使是正确方向的指针。如果您需要任何额外的信息,请询问。
谢谢,杰克。