我正在尝试在登录用户获得积分/积分的网站内设置“结帐/订单”页面 . 一旦他们赚取了一定数量的这些积分,他们就可以去购物车并只用这些积分付款 . (没有钱换手,所以不涉及paypal /结账/运输/税收等) .
我已经完成了'shopping cart'页面和'view cart'页面(查看购物车代码在this page上),这要归功于Steve和KMK;) .
我的MySQL数据库上有两个表,'orders'(包含订单ID,用户ID,总计和时间戳)和'order_contents'(订单内容ID,订单ID,产品ID,数量和价格) . 'total'是总价,'price'是每个产品的价格 .
我试图通过 submit_cart.php 文件(下面的代码)将用户从视图购物车页面中选择的项目(即产品,数量等)放入数据库中的'order'和'order_contents'表中,但它无法正常工作 .
What does work on this code 是它将新行/ order_id放入orders表以及users_id .
What doesn't work: 订单的总价格在数据库上没有't get inserted (shows up as ' 0',它显示第一条错误消息(结尾为1) .
什么都没有插入'order_contents'表,此时我假设它是因为插入'orders'表不起作用或某种方式购物车会话变量不会跨越(?)但我很高兴得到纠正......
如果有人可以伸出援助之手,甚至建议采用不同的方法,请随意!谢谢!
<?php
$page_title = 'Order Confirmation';
include ('./includes/header.html');
if (!isset($_SESSION['users_id'])) {
// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= '/login.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
}
$users = $_SESSION['users_id']; // Temporary.
$total = 0; // Total cost of the order.
require_once ('/MySQL/database.php'); // Connect to the database.
@mysqli_autocommit ($dbc, FALSE);
$query = "INSERT INTO orders (users_id, total) VALUES
($users, $total)";
$result = @mysql_query($query);
if (@mysql_affected_rows($dbc) == 1) {
// Need the order ID.
$oid = @mysql_insert_id($dbc);
// Insert the specific order contents into the database.
$query = "INSERT INTO order_contents (order_id, products_id, quantity, price)
VALUES (";foreach ($_SESSION['cart'] as $pid =>$value) {
$query .= "$oid, $pid, {$value['quantity']}, {$value['price']})";
}
$query = substr($query, 0, -2); // Chop off last two characters.
$result = @mysql_query($query);
// Report on the success.
if (@mysql_affected_rows($dbc) == count($_SESSION['cart'])) { // Whohoo!
// Commit the transaction.
@mysqli_commit($dbc);
@mysql_close($dbc);
// Clear the cart.
unset($_SESSION['cart']);
// Message to the customer.
echo '<p>Thank you for your order.
It has been submitted for processing.</p>';
// Send emails and do whatever else.
} else { // Rollback and report the problem.
@mysqli_rollback($dbc);
@mysql_close($dbc);
echo '<p>Your order could not be processed due to a system error.
You will be contacted in order to have the problem fixed.
We apologize for the inconvenience 1.</p>';
// Send the order information to the administrator.
}
}
else { // Rollback and report the problem.
@mysqli_rollback($dbc);
@mysql_close($dbc);
echo '<p>Your order could not be processed due to a system error.
You will be contacted in order to have the problem fixed.
We apologize for the inconvenience 2.</p>';
// Send the order information to the administrator.
}
?>
</div></div>
<?php
include ('./includes/footer.html');
?>
2 回答
您已将总变量设置为0,这就是为什么订单表上的总成本显示为零的原因 .
这是一个非常简单的示例,它使用您可能觉得有用的存储过程 .
完整的脚本:http://pastie.org/1268992
希望能帮助到你 :)
Example stored procedure calls
Example PHP script
Example MySQL script