I have connected to the database.
I have a shopping cart working in a PHP program, here is the problem:
If I click add item1 to the cart it will add it and if I press it again it will add it again. Then if I press add to cart on item 2 it will add item 1 again but not item 2,then if I press item 2 again it will start to increment item 2 in the cart. I uploaded the 2 pages where I use this, as I am not certain which exact part of the code is causing the problem.
<?php
// add before
session_start();
// connect
require_once("include/db_connect.php");
//
if(isset($_REQUEST['page']) ){
$pages = array("products","cart");
if(in_array($_REQUEST['page'], $pages) ){
$page = $_REQUEST['page'];
}
else{
$page = "products";
}
}
else{
$page = "products";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8" />
</head>
<body>
<div id="sect">
<div style="float:right; width:300px; border:1px solid red;">
<h1>Cart</h1>
<?php
if(isset($_SESSION['cart']) ){
$sql = "SELECT * FROM product WHERE id_number IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
// will take away the last comma
$sql = substr($sql,0,-1) . ") ORDER BY id_number ASC";
$query = mysql_query($sql) or die(mysql_error() );
while($row = mysql_fetch_assoc($query)){
?>
<p><?php echo $row['name']; ?> <?php echo " " . $_SESSION['cart'][$row['id_number']]['quantity']; ?> </p>
<?php
}
}else{
echo " <p>Your cart is empty. <br> Please add some products</p> ";
}
echo "<a href='index.php?page=cart'>Go to Cart</a> ";
?>
</div>
<?php require($page . ".php"); ?>
</div>
</body>
</html>
This is the other class:
<?php
if(isset($_REQUEST['action']) && ($_REQUEST['action'] == "add" )){
$id = intval($_REQUEST['id']);
if(isset($_SESSION['cart'][$id]) ){
$_SESSION['cart'][$id]['quantity']++ ;
}
else{
$sql2 = "SELECT * FROM product WHERE id_number={$id}";
$query2 = mysql_query($sql2);
if(mysql_num_rows($query2) != 0 ){
$row2 = mysql_fetch_array($query2);
$_SESSION['cart'][$row2['id_number']] = array("quantity" => 1, "price" => $row2['price'] ) ;
}
else{
$message = "This product id is invalid" ;
}
}
}
?>
<h2 class="message"><?php if(isset ($message)){echo $message;} ?></h2>
<h1>Product Page</h1>
<!-- 297de5 -->
<table>
<tr>
<th>name</th>
<th>artist</th>
<th>price</th>
<th>Action</th>
</tr>
<?php
$sql = "SELECT * FROM product ORDER BY price ASC";
$query = mysql_query($sql) or die(mysql_error() );
while($row = mysql_fetch_assoc($query) ){
?>
<tr>
<td><?php echo $row['name']; ?> </td>
<td><?php echo $row['artist']; ?> </td>
<td>$<?php echo $row['price']; ?> </td>
<td><a href="index.php?page=products&action=add&id=<?php echo $row['id_number']; ?> "> Add to Cart</a> </td>
</tr>
<?php
}
?>
</table>
Your products.php file is included in the middle of the first file after the cart is already displayed. So if you modify $_SESSION["cart"] in products.php it's only gonna show up in the cart next time you reload the page.
A proper way of handling this would be to apply an MVC pattern or at least to separate your data from your view by using templates.
If you just need a quick hack, consider this code:
// place this in your first file right after you initialize $page
ob_start();
require($page.".php");
$strPageContents = ob_get_clean();
And replace <?php require($page . ".php"); ?>
with <?= $strPageContents; ?>