Hi,
I'm having trouble with my session variable. When I add this code in the while loop it doesn't add the product in, but when I put it outside it adds the product to the cart. Can anyone tell me what's wrong with my code? It would save me a lot of time.
Thanks
<?php
session_start();
$page = 'index.php';
$con = mysqli_connect("****","***","****","***");
if (isset($_GET['add'])) { // If we have clicked on add
$quantity = mysqli_query($con,'SELECT ID,Quantity FROM Products WHERE ID =' .mysqli_escape_string((int) $_GET['add']) );
while ($row = mysqli_fetch_assoc($con,$quantity)){
if ($row['Quantity'] !=$_SESSION['cart_'.$_GET['add']]);
}
$_SESSION['cart_'] += 1;
}
function products () {
$con = mysqli_connect("*****","****","***","***");
mysqli_select_db("Products", $con);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$result = mysqli_query($con,'SELECT ID,Name,Description,Price FROM Products WHERE ID >= 0');
echo "<table border='1'>
<tr>
<th> ID </th>
<th> Products </th>
<th> Description </th>
<th> Price </th>
<th> Purchase </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>" . "<td>" . $row['ID'] . "</td>" . "<td>" . $row['Name'] . "</td>" . "<td>" . $row['Description'] . "</td>" . "<td>" . number_format($row ['Price'], 2) . "<td>" . '<form action="add.php?add='.$row['ID'] .'" method="POST"><input type="submit" value="Add" name="add"> </form>' . "</td>" . "</tr>";
}
}
echo $_SESSION['cart_'];
?>