购物车/购物篮会话不会进入数据库:PHP MySQL
我正在用 php/mysql 编写一个程序,其中登录用户使用积分系统通过购物车订购商品(不涉及金钱、贝宝、信用卡付款、运费税等 - 仅积分)。我的 php 知识是基础到中低级的。
我有下面两个脚本:
名为 view_cart.php 的购物车/购物篮 这工作正常(如果没有足够的积分,则禁用结账)。
订单提交到名为 submit_order.php
我在将购物车会话链接到提交时遇到问题订单页面/数据库。我收到第一条错误消息,但它在订单表下创建了一个新订单,但仅使用登录的用户 ID 号,而总数为 0。 order_contents 表也没有任何反应。
我猜这与“购物车”会话变量/数组有关,因此如果有人可以帮助或引导我朝正确的方向前进,那就太好了。
谢谢
下面的view_cart.php:
<?php //view_cart.php
$page_title = 'ViewCart';
include ('./includes/header.html');
if (!isset($_SESSION['users_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/login.php';
ob_end_clean();
header("Location: $url");
exit();
}
$rwp = $_SESSION['points'];
$problem = FALSE;
if (isset($_POST['submitted']))
{
foreach ($_POST['qty'] as $k => $v) {
$pid = (int) $k;
$qty = (int) $v;
if ( $qty == 0 ) {
unset ($_SESSION['cart'][$pid]);
} elseif ( $qty > 0 ) {
$_SESSION['cart'][$pid] ['quantity'] = $qty;
}
}
}
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key =>$value) {
if (isset($value)) {
$empty = FALSE;
break;
}
}
}
if (!$empty) {
require_once ('mysql_connect.php');
$query = "SELECT users_id, points FROM user_points
WHERE user_points.users_id = users.users_id";
$result = mysql_query($query);
$query = "SELECT products_id, products_name FROM categories, products
WHERE categories.categories_id = products.categories_id AND products.products_id
IN (";foreach ($_SESSION['cart'] as $pid =>$value) {
$query .= $pid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY categories.categories_name ASC';
$result = mysql_query($query);
echo '
<table border="0" width="100%" cellspacing="1" cellpadding="5"
align="center">
<tr class="top">
<td align="left" width="46%"><b>Product</b></td>
<td align="right" width="18%"><b>Price</b></td>
<td align="center" width="16%"><b>Qty</b></td>
<td align="right" width="20%"><b>Sub Total</b></td>
</tr>
<form action="view_cart.php" method="post">
';
$total = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$subtotal = $_SESSION['cart'][$row
['products_id']]['quantity'] *
$_SESSION['cart'][$row ['products_id']]['price'];
$total += $subtotal;
echo " <tr>
<td align=\"left\">{$row['products_name']}</td>
<td align=\"right\">{$_SESSION['cart'][$row['products_id']] ['price']} pts</td>
<td align=\"center\"><input type=\"text\" size=\"3\"
name=\"qty[{$row['products_id']}]\"
value=\"{$_SESSION['cart'][$row['products_id']]['quantity']}\" /></td>
<td align=\"right\">" . number_format ($subtotal) . " pts</td>
</tr>\n";
}
mysql_close($dbc);
$str = '<tr class="even">
<td colspan="3" align="right"><b> TOTAL:<b></td>
<td align="right"><b>' . number_format ($total) . ' pts </b></td>
</tr>
</table>
<br />
<div align="center"><input type="submit" name="submit" value="Update" />
<input type="hidden" name="submitted" value="TRUE" />
</form><br /><br /></div>';
if($up >= $total) {
$str .='<a href="submit_order.php">Submit Order</a></p>';
}
else {
$str .='<p>You do not have enough points to proceed to checkout</p>';
}
echo $str;
} else {
echo '<p>Your cart is currently empty.</p>';
}
?>
<?php
include ('./includes/footer.html');
?>
这是submit_order.php脚本。
<?php
$page_title = 'Order Confirmation';
include ('./includes/header.html');
if (!isset($_SESSION['users_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/login.php';
ob_end_clean();
header("Location: $url");
exit();
}
$users = $_SESSION['users_id']; // Temporary.
$total = 0;
require_once ('mysql_connect.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) {
$oid = @mysql_insert_id();
$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);
$result = @mysql_query($query);
if (@mysql_affected_rows($dbc) == count($_SESSION['cart'])) {
@mysqli_commit($dbc);
@mysql_close($dbc);
unset($_SESSION['cart']);
echo '<p>Thank you for your order.
It has been submitted for processing.</p>';
} else {
@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>';
}
}
else {
@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>';
}
?>
</div></div>
<?php
include ('./includes/footer.html');
?>
I am writing a program with php/mysql in which logged in users use a points system to order items through a shopping cart (no money,paypal,credit card payments,shipping taxes etc. are involved - points only). My php knowledge is basic to low intermediate.
I have two scripts below:
The shopping cart/basket called view_cart.php
This works fine (and disables checkout if they don't have enough points).Order submission to database called submit_order.php
I am having trouble with linking the shopping cart session to the submit order page/database. I get the first error message but it creates a new order under the orders table but only with the logged in users ID number, while the total comes to 0. Nothing happens with the order_contents table either.
I am guessing it is something to do with the 'cart' session variables/array so if someone could help or steer me in the right direction that would be great.
Thanks A
view_cart.php below:
<?php //view_cart.php
$page_title = 'ViewCart';
include ('./includes/header.html');
if (!isset($_SESSION['users_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/login.php';
ob_end_clean();
header("Location: $url");
exit();
}
$rwp = $_SESSION['points'];
$problem = FALSE;
if (isset($_POST['submitted']))
{
foreach ($_POST['qty'] as $k => $v) {
$pid = (int) $k;
$qty = (int) $v;
if ( $qty == 0 ) {
unset ($_SESSION['cart'][$pid]);
} elseif ( $qty > 0 ) {
$_SESSION['cart'][$pid] ['quantity'] = $qty;
}
}
}
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key =>$value) {
if (isset($value)) {
$empty = FALSE;
break;
}
}
}
if (!$empty) {
require_once ('mysql_connect.php');
$query = "SELECT users_id, points FROM user_points
WHERE user_points.users_id = users.users_id";
$result = mysql_query($query);
$query = "SELECT products_id, products_name FROM categories, products
WHERE categories.categories_id = products.categories_id AND products.products_id
IN (";foreach ($_SESSION['cart'] as $pid =>$value) {
$query .= $pid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY categories.categories_name ASC';
$result = mysql_query($query);
echo '
<table border="0" width="100%" cellspacing="1" cellpadding="5"
align="center">
<tr class="top">
<td align="left" width="46%"><b>Product</b></td>
<td align="right" width="18%"><b>Price</b></td>
<td align="center" width="16%"><b>Qty</b></td>
<td align="right" width="20%"><b>Sub Total</b></td>
</tr>
<form action="view_cart.php" method="post">
';
$total = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$subtotal = $_SESSION['cart'][$row
['products_id']]['quantity'] *
$_SESSION['cart'][$row ['products_id']]['price'];
$total += $subtotal;
echo " <tr>
<td align=\"left\">{$row['products_name']}</td>
<td align=\"right\">{$_SESSION['cart'][$row['products_id']] ['price']} pts</td>
<td align=\"center\"><input type=\"text\" size=\"3\"
name=\"qty[{$row['products_id']}]\"
value=\"{$_SESSION['cart'][$row['products_id']]['quantity']}\" /></td>
<td align=\"right\">" . number_format ($subtotal) . " pts</td>
</tr>\n";
}
mysql_close($dbc);
$str = '<tr class="even">
<td colspan="3" align="right"><b> TOTAL:<b></td>
<td align="right"><b>' . number_format ($total) . ' pts </b></td>
</tr>
</table>
<br />
<div align="center"><input type="submit" name="submit" value="Update" />
<input type="hidden" name="submitted" value="TRUE" />
</form><br /><br /></div>';
if($up >= $total) {
$str .='<a href="submit_order.php">Submit Order</a></p>';
}
else {
$str .='<p>You do not have enough points to proceed to checkout</p>';
}
echo $str;
} else {
echo '<p>Your cart is currently empty.</p>';
}
?>
<?php
include ('./includes/footer.html');
?>
Here is the submit_order.php script.
<?php
$page_title = 'Order Confirmation';
include ('./includes/header.html');
if (!isset($_SESSION['users_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/login.php';
ob_end_clean();
header("Location: $url");
exit();
}
$users = $_SESSION['users_id']; // Temporary.
$total = 0;
require_once ('mysql_connect.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) {
$oid = @mysql_insert_id();
$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);
$result = @mysql_query($query);
if (@mysql_affected_rows($dbc) == count($_SESSION['cart'])) {
@mysqli_commit($dbc);
@mysql_close($dbc);
unset($_SESSION['cart']);
echo '<p>Thank you for your order.
It has been submitted for processing.</p>';
} else {
@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>';
}
}
else {
@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>';
}
?>
</div></div>
<?php
include ('./includes/footer.html');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看不到任何地方,您是从父级包含文件中调用它吗?
我在这里
I can't see
Anywhere in here, are you calling it from a parent including file?
我猜你把
session_start()
放在了错误的地方。它必须在标记之前调用。
I guess you put
session_start()
in the wrong place. It must be called before the<html>
tag.