Hi
I’m trying to generate a shopping cart items list, but when I submit/add to cart then only 1 product seems to be in.
I don’t see what is wrong.
http://maxander.byethost9.com/veebipood.php
function product_display(){
global $product, $rowcount, $dbc;
// Get the data from MySQL database and generate the product table
$query = "SELECT kaup_id, kaup, hind, yhik FROM kaubad";
$data = mysqli_query($dbc, $query) or die('Data not inserted DB_HOST');
while ($row = mysqli_fetch_array($data)){
array_push($product, array($row['kaup'], $row['hind'], $row['yhik'], $row['kaup_id']));
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<table>
<tr> <th>Product:</th> <td>'.$product[$rowcount][0].'</td> </tr>
<tr> <th>Price:</th> <td>'.$product[$rowcount][1].' EUR</td> </tr>
<tr> <th>Quantity:</th> <td><input type="text" name="quantity" /></td> <td>'.$product[$rowcount][2].'</td> </tr>
<tr> <td>
<input type="hidden" name="product_id" id="product_id" value="'.$product[$rowcount][3].'" />
<input type="submit" name="submit" value="Add to cart">
</td></tr>
</table>
</form>';
$rowcount++;
}
mysqli_close($dbc);
}
product_display();
if(!($_GET['remove_product'])){
$key = $_GET['remove_product'];
unset($_SESSION['order'][$key]);
}
if($_SERVER['REQUEST_METHOD'] == 'POST' && is_numeric($_POST['quantity']) && ($_POST['quantity'] <= 999)){
$product_id = trim($_POST['product_id']) - 1;
$quantity = trim($_POST['quantity']);
array_push($_SESSION['order'], array($product[$product_id][0], $product[$product_id][1], $product[$product_id][2], $quantity, $product_id));
$ordercount = count($_SESSION['order']) - 1;
echo'<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<table>
<tr> <th>Please fill this order form</th></tr>
<tr> <th>First name:</th> <td><input type="text" name="firstname" /></td> </tr>
<tr> <th>Last name:</th> <td><input type="text" name="lastname" /></td> </tr>
<tr> <th>Email:</th> <td><input type="text" name="email" /></td> </tr>
<tr><th>CART</th></tr>
<tr> <th>Product:</th> <th>Quantity</th> <th>Sum</th> </tr>';
for ($i = 0; $i <= $ordercount; $i++) {
echo'<tr> <td>'.$_SESSION['order'][$i][0].'</td>
<td>'.$_SESSION['order'][$i][1].'</td>
<td>'.($_SESSION['order'][$i][1]*$_SESSION['order'][$i][3]).'</td>
<td><a href="veebipood.php?remove_product='.$i.'">Remove</a></td> </tr>';
}
echo'<tr> <td><input type="submit" name="order" value="Send order"></td></tr>
</table>
</form>';
}
Thank you