Jump to content
Larry Ullman's Book Forums

woodsie2523

Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by woodsie2523

  1. I have gone over this again and again and cannot understand the problem. From Chapter 17 I created all the pages. They all work except for view_cart.php and checkout.php. I can upload prints, browse prints, add a print to my cart, but when I am on the view_cart.php page it will not update the quantity when I change it to 0 or any quantity. When I hit refresh the original quantity returns. I did look at the errata page, nothing that helps me there. I also downloaded the code from the website and mine is the same. What am I missing here? No error messages.

     

    <?php # Script 17.9 - view_cart.php - woodsie - 12-7-11

     

    // This page displays the contents of the shopping cart

    // This page also lets the user update the contents of the cart

    // Set the page title and include the HTML header:

    $page_title = 'View Your Shopping Cart';

    include ('includes/header.html');

    // Check if the form has been submitted (to update the cart):

    if (isset($_POST['submitted'])) {

    // Change any quantities:

    foreach ($_POST['qty'] as $k => $v) {

    // Must be integers!

    $pid = (int) $k;

    $qty = (int) $v;

     

    if ( $qty == 0 ) { // Delete

    unset ($_SESSION['cart'][$pid]);

    } elseif ( $qty > 0 ) { // Change quantity

    $_SESSION['cart'][$pid]['quantity'] = $qty;

    }

     

    } // End of FOREACH

    } // End of SUBMITTED IF

    // Display the cart if it's not empty

    if (!empty($_SESSION['cart'])) {

    // Retrieve all of the information for the prints in the cart:

    require_once ('../../mysqli_connect.php');

    $q = "SELECT print_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name

    FROM artists, prints

    WHERE artists.artist_id = prints.artist_id AND prints.print_id IN (";

    foreach ($_SESSION['cart'] as $pid => $value) {

    $q .= $pid . ',';

    }

    $q = substr($q, 0, -1) . ') ORDER BY artists.last_name ASC';

    $r = mysqli_query ($dbc, $q);

     

    // Create a form and a table:

    echo '<form action="view_cart.php" method"post">

    <table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">

    <tr>

    <td align="left" width="30%"><b>Artist</b></td>

    <td align="left" width="30%"><b>Print Name</b></td>

    <td align="right" width="10%"><b>Price</b></td>

    <td align="center" width="10%"><b>Qty</b></td>

    <td align="right" width="10%"><b>Total Price</b></td>

    </tr>

    ';

    // Print each item

    $total = 0; // Total cost of the order

    while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

     

    // Calculate the total and sub-totals

    $subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];

    $total += $subtotal;

     

    // Print the row

    echo "\t<tr>

    <td align=\"left\">{$row['artist']}</td>

    <td align=\"left\">{$row['print_name']}</td>

    <td align=\"right\">\${$_SESSION['cart'][$row['print_id']]['price']}</td>

    <td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]['quantity']}\" /></td>

    <td align=\"right\">$" . number_format ($subtotal, 2) . "</td>

    </tr>\n";

     

    } // End of the WHILE loop

     

    mysqli_close($dbc); // Close the database connection

    // 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" /></div>

    <input type="hidden" name="submitted" value="TRUE" />

    </form><p align="center">Enter a quantity of 0 to remove an item.

    <br /><br /><a href="checkout.php">Checkout</a></p>';

    } else {

    echo '<p>Your cart is currently empty.</p>';

    }

    include ('includes/footer.html');

    ?>

     

    and here is checkout.php

    <?php # Script 17.10 - checkout.php - woodsie - 12-4-11

     

    // This page inserts the order information into the table

    // This page would come after the billing process

    // This page assumes that the billing process worked (the money has been taken)

     

    // Set the page title and include the HTML header

    $page_title = 'Order Confirmation';

    include ('includes/header.html');

    // Assume that the customer is logged in and that this page has access to the customer's ID

    $customer = 2; // Temporary

    // Assume that this page receives the order total

    $total = 200.93; // Temporary

    require_once ('../../mysqli_connect.php'); // Connect to the database

    // Turn autocommit off

    mysqli_autocommit($dbc, FALSE);

    // Add the order to the orders table

    $q = "INSERT INTO orders (customer_id, total) VALUES ($customer, $total)";

    $r = mysqli_query($dbc, $q);

    if (mysqli_affected_rows($dbc) == 1) {

    // Need the order ID

    $oid = mysqli_insert_id($dbc);

     

    // Insert the specific order contents into the database

     

    // Prepare the query

    $q = "INSERT INTO order_contents (order_id, print_id, quantity, price) VALUES (?, ?, ?, ?)";

    $stmt = mysqli_prepare($dbc, $q);

    mysqli_stmt_bind_param($stmt, 'iiid', $oid, $pid, $qty, $price);

     

    // Execute each query, count the total affected

    $affected = 0;

     

    foreach ($_SESSION['cart'] as $pid => $item) {

    $qty = $item['quantity'];

    $price = $item['price'];

    mysqli_stmt_execute($stmt);

    $affected += mysqli_stmt_affected_rows($stmt);

    }

    // Close this prepared statement

    mysqli_stmt_close($stmt);

    // Report on the success

    if ($affected == count($_SESSION['cart'])) { // Whohoo!

     

    // Commit the transaction

    mysqli_commit($dbc);

     

    // Clear the cart

    unset($_SESSION['cart']);

     

    // Message to the customer

    echo '<p>Thank you for your order. You will be notified when the items ship.</p>';

     

    // Send emails and do whatever else

     

    } else { // Rollback and report the problem

     

    mysqli_rollback($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.</p>';

     

    // Send the order information to the administrator

     

    }

    } else { // Rollback and report the problem

    mysqli_rollback($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.</p>';

     

    // Send the order information to the administrator

     

    }

    mysqli_close($dbc);

    include ('includes/footer.html');

    ?>

×
×
  • Create New...