php shopping cart with php

I have my code and it (sort of ) works.

First problem I have is it only adds the first item. It will not add more then one item also.

In other words it will only add the top information from the index.php file to the viewCart.php.

second problem - When i go to remove an item from the cart it works but it will take me negative and I dont want this

Below is my code- Thanks for all the help

[php]<?php
$db_host = “localhost”;
$db_username = “root”;
$db_password = “root”;
$db_name = “Catalog”;

$con=mysqli_connect($db_host, $db_username, $db_password,$db_name);
echo"Connect Successful";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,“SELECT * FROM items”);

echo "

";

while($row = mysqli_fetch_array($result))
{
echo “

”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
}
echo “
itemID description price imagePath
” . $row[‘itemID’] . “ ” . $row[‘description’] . “ ” . $row[‘price’] . “ Add
this to my cart!
”;

mysqli_close($con);
echo “View Cart
?>[/php]

[php]<?php
session_start();
if(isset($_GET[‘itemID’]))
{
$itemID = $_GET[‘itemID’];
}
else
{
$itemID = 1;
}

if(isset($_GET[‘action’]))
{
$action = $_GET[‘action’];
}
else
{
$action = “empty”;
}

switch ($action)
{
case “add”:
if(isset($_SESSION[‘cart’][$itemID])){
$_SESSION[‘cart’][$itemID]++;
}
else{
$_SESSION[‘cart’][$itemID]=1;
}
break;
case “remove”:
if(isset($_SESSION[‘cart’][$itemID])){
$_SESSION[‘cart’][$itemID]–;
if(isset($_SESSION[‘cart’][$itemID])==0){
unset($_SESSION[‘cart’][$itemID]);
}
}

   break;

case “empty”:
unset($_SESSION[‘cart’]);
break;

}

?>

View Shopping Cart [/php]

[php]<?php
session_start();

echo “

” .‘Shopping Cart Contents’."

";

if(isset($_SESSION['cart'])){

echo "<table>";
$total =0;
   foreach ($_SESSION['cart'] as $item => $quantity){
     $db_host = "localhost";
    $db_username = "root";
    $db_password = "root";
    $db_name = "Catalog";

$con=mysqli_connect($db_host, $db_username, $db_password,$db_name);
echo"Connect Successful";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,“SELECT * FROM items WHERE itemID = $item”);
$row = mysqli_fetch_array($result);

     $description = $row['description'];
     $price = $row['price'];
     $imagePath = $row['imagePath']; 
     $total = $price * $quantity;
     echo '<tr>';
            echo "<td><img src = '". $imagePath . "'/></td>";
             echo "<td>". $description . "</td></br>";
             echo "<td>". $price . "</td>";
             echo "<td>".$item."</td>";
             echo "<td>".$quantity."<a href='updateCart.php?itemID=".$item."&quantity=".$quantity."&action=remove'>Remove Item</a></td>";
             
             
             echo '</tr>';
            

             
              
  }

 echo "</table>";
 
 

}


else{
    echo "<h2>No items in cart.</h2>";

} 
 
    
 echo "<a href='updateCart.php?&quantity=&action=empty'>Empty Shopping Cart</a>";      

?>

View Cart

Back to Catalog

[/php]

A07_QueryItems.zip (22.3 KB)

Why do you send in quantity when you do not use it server side?

going negative:

[php]if (isset($_SESSION[‘cart’][$itemID])) {
$_SESSION[‘cart’][$itemID]–;
if (isset($_SESSION[‘cart’][$itemID])==0) {
unset($_SESSION[‘cart’][$itemID]);
}
}[/php]

This will never do the unset line.
[php]if (isset($_SESSION[‘cart’][$itemID])==0) {[/php]
reads like: if ((true) == 0)

Change it to
[php]if ($_SESSION[‘cart’][$itemID] < 1) {[/php]

Cool I see now …Now why do I seem to run into a problem that it only adds one item. It adds the quantity to the total but you could never have multiple items displayed. It always just use the same item for whatever item you say add from. Just adds quantity.

Sponsor our Newsletter | Privacy Policy | Terms of Service