我正在尝试在登录用户赚取积分/积分的站点内设置“结帐/订购”页面。一旦他们获得了一定数量的这些积分,他们就可以进入购物车并仅使用这些积分付款。(没有钱易手,所以不涉及贝宝/结帐/运费/税等)。
我已经完成了“购物车”页面和“查看购物车”页面(查看购物车代码在此页面上),感谢 Steve 和 KMK ;)。
我的 MySQL 数据库中有两个表,“订单”(具有订单 ID、用户 ID、总数和时间戳)和“订单内容”(订单内容 ID、订单 ID、产品 ID、数量和价格)。“total”是总价,“price”是每个产品的价格。
我正在尝试通过submit_cart.php文件(下面的代码)将用户从查看购物车页面选择的项目(即产品、数量等)放入数据库中的“订单”和“订单内容”表中,但它不是t 正常工作。
这段代码的作用在于它将新的行/order_id 以及 users_id 放入订单表中。
什么不起作用:订单的总价格没有被插入(在数据库中显示为“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');
?>