Reputation: 13
I tried searching in google for codes in shopping cart using php ang mysql. when I click the "add to cart" button nothing shows up in my cart.php. please help me.
this is my product.php
<a class="btn btn-info btn-fill pull-right" href="index.php?page=inso94&action=add&prod_id=<?php echo $row['0'];?>">ADD TO CART</a>
and this is my cart.php
<?php
$prod_id = $_GET["prod_id"]; //the product id from the URL
$action = $_GET["action"]; //the action from the URL
switch($action) { //decide what to do
case "add":
$_SESSION['cart'][$prod_id]++; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$prod_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$prod_id] == 0) unset($_SESSION['cart'][$prod_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
if($_SESSION['cart']) {
foreach($_SESSION['cart'] as $prod_id => $prod_quantity) {
$sql = mysql_query("SELECT * FROM product WHERE id = '$prod_id'");
if(isset($res)) {
while($result = mysql_fetch_assoc($sql)) {
$line_cost = $prod_price * $prod_quantity;
$total = $total + $line_cost;
?>
<tr>
<td><?php echo $result["prod_name"]; ?></td>
<td><?php echo $result["prod_code"]; ?></td>
<td><?php echo $result["prod_category"]; ?></td>
<td><?php echo $result["prod_price"]; ?></td>
<td><?php echo $result["prod_quantity"]; ?></td>
<td><?php echo "<a href='$_SERVER[PHP_SELF]$action=remove&prod_id={$result['prod_id']}' class='btn btn-warning btn-fill btn-sm pull-right'>Remove</a>"; ?></td>
<td><?php echo $line_cost; ?></td>
</tr>
<?php
}
}
}
} else {
echo "You have no items in your shopping cart.";
}
?>
Upvotes: 0
Views: 683
Reputation: 764
You are adding 1 to the product id but you have not initialized it. First, add the following statement to the top of your code to start the session:
session_start();
Now check if the $_SESSION['cart'] exists or not. If not, create it.
if(!isset($_SESSION['cart'])
$_SESSION['cart'] = array();
Inside your switch statement when you want to add, check if the product already exists in the cart then add one to it, otherwise initialize it with 1.
if(isset($_SESSION['cart'][$prod_id]))
$_SESSION['cart'][$prod_id]++;
else
$_SESSION['cart'][$prod_id] = 1;
You haven't initialized total as well. Some of the logic of your code in kind of wrong. The while loop makes no sense since there should be only in product for each id. The while loop should be if statement.
Upvotes: 1
Reputation: 1258
You query and result set up looks all wrong.
$sql = mysql_query("SELECT * FROM product WHERE id = '$prod_id'");
if(isset($res)) {
while($result = mysql_fetch_assoc($sql)) {
$line_cost = $prod_price * $prod_quantity;
$total = $total + $line_cost;
?>
<tr>
<td><?php echo $result["prod_name"]; ?></td>
<td><?php echo $result["prod_code"]; ?></td>
<td><?php echo $result["prod_category"]; ?></td>
<td><?php echo $result["prod_price"]; ?></td>
<td><?php echo $result["prod_quantity"]; ?></td>
<td><?php echo "<a href='$_SERVER[PHP_SELF]$action=remove&prod_id={$result['prod_id']}' class='btn btn-warning btn-fill btn-sm pull-right'>Remove</a>"; ?></td>
<td><?php echo $line_cost; ?></td>
</tr>
<?php
}
}
}
} else {
I think this is what you want?
$result = mysql_query("SELECT * FROM product WHERE id = '$prod_id'");
if(isset($result)) {
while($row = mysql_fetch_assoc($result)) {
$prod_price = $row['prod_price']
$line_cost = $prod_price * $prod_quantity;
$total = $total + $line_cost;
?>
<tr>
<td><?php echo $row ["prod_name"]; ?></td>
<td><?php echo $row ["prod_code"]; ?></td>
<td><?php echo $row ["prod_category"]; ?></td>
<td><?php echo $row ["prod_price"]; ?></td>
<td><?php echo $row ["prod_quantity"]; ?></td>
<td><?php echo "<a href='$_SERVER[PHP_SELF]$action=remove&prod_id={$result['prod_id']}' class='btn btn-warning btn-fill btn-sm pull-right'>Remove</a>"; ?></td>
<td><?php echo $line_cost; ?></td>
</tr>
<?php
}
}
}
} else {
Upvotes: 0