I am trying to setup a “checkout/order” page within a site in which logged in users earn points/credits. Once they earn a certain amount of these points they can then go to a shopping cart and pay with these points only. (No money changes hands, so no paypal/checkout/shipping/taxes etc are involved).
I have done a 'shopping cart' page and 'view cart' page (view cart code is on this page), which works fine thanks to Steve and KMK ;).
I have two tables on my MySQL database, 'orders' (which has order id, users id, total & time stamp) and 'order_contents' (order contents id, order id, product id, quantity and price). 'total' is total price, 'price' is price per product.
I am trying to get the items that the user selects (ie products, quantity etc) from the view cart page into the 'order' and 'order_contents' tables in the database via the submit_cart.php file (code below) but it isn't working properly.
What does work on this code is that it puts a new row/order_id into the orders table, as well as the users_id.
What doesn't work: The total price of the order doesn't get inserted (shows up as '0' on the database) and it displays the first error message (with the 1 on the end).
Nothing gets inserted into the 'order_contents' table either, at this point I am assuming it is because the insert into the 'orders' table isn't working or somehow the cart session variables are not going across(?) but I am happy to be corrected...
If someone could lend a hand or even suggest a different approach please feel free! Thanks!
<?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');
?>