Hi everyone! I am new to PHP and am creating a prototype for a school project. When I add items to a cart in session they are being added successfully, however, I can’t get them to actually appear in the cart. The cart is created, and the item says “item is already in cart” when I try to add it again.
Parts of this code are pre-written for me from the textbook we’re using, and then I am to edit them to use the MySQL database and the design of my project and scope. This is what I have for the page where my cart appears:
<?php
session_start();
//check session first
if (!isset($_SESSION['email'])){
include ("../includes/header.php");
echo "You are not logged in!<br /><br />";
echo ("<a href=../Home/index.php class='buttons2'>Login</a>");
exit();
}else{
//include the header
include ('../includes/header.php');
require_once ('../../mysqli_connect.php');
echo ("<center>");
echo ("<h3>Rentals</h3>");
echo ("<div class='buttons2'><a href=searchformrentals.php class='buttons2'>Search Rentals</a></div>");
// Check if the form has been submitted (to update the cart):
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 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 ('../../mysqli_connect.php'); // Connect to the database.
$q = "SELECT title, price FROM movies, categories WHERE movies.category_id = categories.id IN (";
foreach ($_SESSION['cart'] as $pid => $value) {
$q .= $pid . ',';
}
$q = substr($q, 0, -1) . ') ORDER BY title ASC';
$r = mysqli_query ($dbc, $q);
// Create a form and a table:
echo '<form action="index.php" method="post">
<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>Title</b></td>
<td align="left" width="30%"><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['title']]['quantity'] * $_SESSION['cart'][$row['price']];
$total += $subtotal;
// Print the row:
echo "\t<tr>
<td align=\"left\">{$row['title']}</td>
<td align=\"left\">{$row['price']}</td>
<td align=\"right\">\${$_SESSION['cart'][$row['title']]['price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['title']}]\" value=\"{$_SESSION['cart'][$row['title']]['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 total, 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><br />
<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
</form><p align="center">Enter a quantity of 0 to remove an item.
<br /><br /><a href="checkout.php" class="buttons2">Checkout</a></p>';
} else {
echo '<p>Your cart is currently empty.</p>';
}
echo ("</center></html>");
}
echo '</div>';
echo '</div>';
include ('../includes/footer.php');
?>