Hello All-
I am using MySQL 4 with PHP 4.3.11
I am building a shopping cart that has a limited quantity of items (class courses).
I have several variables for maximum seats, current seats, available seats ($max_seats, $curr_seats, and avail_seats).
I have created an add to cart function that subtracts from available seats and adds to current seats that runs fine. The problem is I am running into a snag when it comes to viewing the cart, which has the option to add to, or delete items from the cart. How do I UPDATE the database from there? If a user wants to take the course out of the cart, it should add the available seat back to the database, and subtract a seat from current seats.
Any help would be appreciated
Here are my two codes:
add to cart:
<?php #add_cart.php
// This page adds courses to the cart.
require_once ('ce_includes/config.inc');
require_once ('../../mysql_connect.php'); // Connect to the database.
$max_seats = NULL;
$avail_seats = NULL;
if (is_numeric ($_GET['id'])) { // Check for a course ID.
$ci = $_GET['id'];
// Set the page title and include the HTML header.
$page_title = 'ACA Community Education | Add to Cart';
include_once ('ce_includes/ce_header.html');
$query = "SELECT * FROM course_table WHERE course_id = '$ci'";
$result = @($query);
$row = mysql_fetch_array($result);
// Check if the cart already contains one of these courses.
if (isset ($SESSION['cart'][$ci])) {
$qty = $SESSION['cart'][$ci] + 1;
} else {
$qty = 1;
}
// Add to the cart session variable.
$_SESSION['cart'][$ci] = $qty;
$row['curr_seats'] = ($row['curr_seats'] + "$qty");
$avail_seats = ($row['max_seats'] - $row['curr_seats']);
$query = "UPDATE course_table SET avail_seats ='$avail_seats', curr_seats ={$row['curr_seats']} WHERE course_id ='$ci'";
$r = mysql_query ($query);
$query = "SELECT * FROM course_table WHERE course_id = '$ci'";
$result = @($query);
$row2 = mysql_fetch_array($result);
// Display a message.
echo "<p>The course <b><i>{$row['course_1title']}</b></i> has been added to your shopping cart.</p>";
echo "maximum seats {$row2['max_seats']}, current seats {$row2['curr_seats']}, available seats {$row2['avail_seats']}";
include_once ('ce_includes/ce_footer.html'); // Require the HTML footer.
} else { // Redirect
header ("Location: http://" . $SERVER['HTTP_HOST'] . dirname($SERVER['PHP_SELF']) . "/index.php");
exit();
}
?>
Here is my view cart code:
<?php
// Contents of the shopping cart.
// Set the page title and include the HTML header.
$page_title = 'ACA Community Education | View Your Shopping Cart';
include_once ('ce_includes/ce_header.html');
//connect to database
require_once ('../../mysql_connect.php'); // Connect to the database.
$query = "SELECT * FROM course_table WHERE course_id = '$ci'";
$result = @($query);
$row = mysql_fetch_array($result);
// Check if the form has been submitted (to update the cart).
if (isset ($POST['submit'])) {
foreach ($POST['qty'] as $key => $value) {
if ( ($value == 0) AND (is_numeric ($value)) ) {
$row['curr_seats'] =($row['curr_seats'] - $POST['qty']);
$row['avail_seats'] =($row['avail_seats'] + $POST['qty']);
$query = "UPDATE course_table SET avail_seats ='$avail_seats', curr_seats ={$row['curr_seats']} WHERE course_id ='$ci'";
$r = mysql_query ($query);
unset ($_SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) ) {
$_SESSION['cart'][$key] = $value;
}
}
}
// Check if the shopping cart is empty.
$empty = TRUE;
if (isset ($SESSION['cart'])) {
foreach ($SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}
}
// Display the cart if it's not empty.
if (!$empty) {
//pull discount code from user and apply to cart
$query = "SELECT disc_code FROM student_table WHERE user_id = '$id'";
$result = @($query);
$row = mysql_fetch_array($result);
$row['disc_code'];
if ($disc_code =='FAC' or 'STF')
{
$disc_rate = '1';
}
else
{
$disc_rate = '0';
}
// Retrieve all of the information for the courses in the cart.
$query = 'SELECT * FROM instructor_table, course_table WHERE instructor_table.instructor_id = course_table.instructor_id AND course_table.course_id IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY instructor_table.last_name ASC';
$result = mysql_query ($query);
// Create a table and a form.
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="20%"><b>Instructor</b></td>
<td align="left" width="20%"><b>Code/ Course</b></td>
<td align="left" width="20%"><b>Seats Available</b></td>
<td align="right" width="10%"><b>Price</b></td>
<td align="right" width="10%"><b>Fees</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="10%"><b>Total Price</b></td>
</tr>
<form action="view_cart_script.php" method="post">
';
// Print each item.
$total = 0; // Total cost of the order.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
// Calculate the discounts, additional fees,total and sub-totals.
$discount = "$disc_rate" $row['course_tuition'];
$course_tuition = ($row['course_tuition'] - "$discount") + $row['add_fees'];
$subtotal = $_SESSION['cart'][$row['course_id']] "$course_tuition";
$total += $subtotal;
// Print the row.
echo " <tr>
<td align=\"left\">{$row['first_name']} {$row['middle_name']} {$row['last_name']}</td>
<td align=\"left\">{$row['course_code']} {$row['course_1title']}</td>
<td align=\"left\">{$row['avail_seats']} out of {$row['max_seats']}</td>
<td align=\"right\">\${$row['course_tuition']}</td>
<td align=\"right\">\${$row['add_fees']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['course_id']}]\" value=\"{$_SESSION['cart'][$row['course_id']]}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // End of the WHILE loop.
// Print the footer, close the table, and the form.
echo ' <tr>
<td colspan="4" align="right"><b>Total:<b></td>
<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update My Cart" /></form><br /><br /><a href="checkout.php"><font size="+3">Checkout</font></a></div>';
mysql_close(); // Close the database connection.
} else {
echo '<p>Your cart is currently empty.</p>';
}
include_once ('ce_includes/ce_footer.html'); // Require the HTML footer.
?>