I have create the PHP for customer order the items that fetch the data on my database. Then it will have the checkbox for customer to select order the item. At this point, currently my system still can proceed the order if customer haven’t check any checkbox and click order button, and it will insert into my database but blank, no data, only orderID is insert. There i need is error message that need check atleast one checkbox to proceed order.
This is my selection item to order.
<?php
include 'connection.php'; //create a new connection to the DB
$query = "SELECT * FROM product_pc"; // $query as string, stores the SQL query
$result = mysql_query($query);
// this is an error handling code
if($result === FALSE) {
die(mysql_error());
}
?>
<form method="post" action="orderpc.php">
<table width='900'>
<tr>
<th></th>
<th><h1>Product Name</h1></th>
<th><h1>Product Image</h1></th>
<th><h1>Price ($)</h1></th>
<th><h1>Quantity</h1></th>
</tr>
<?php
while($menu = mysql_fetch_array($result)){
?>
<tr>
<td><input type="checkbox" name="checkbox[]" value="<?=$menu['ProductID'];?>"/></td>
<td><?=$menu['ProductName'];?></td>
<td><img src='<?=$menu['ProductImg'];?>'width="120" height="120"/></td>
<td><?=$menu['ProductPrice'];?></td>
<td><input type="text" name="<?=$menu['ProductID'];?>" placeholder="Quantity" style="text-align: center"/></td>
</tr>
<?php
}
?>
</table>
<br><br>
<input type='submit' value='Proceed With Order' name='order'/>
and below is my proceed order button coding.
<?php
include 'connection.php';
$amount = 0;
$date = date("Y-m-d");
if(isset($_POST['order'])) {
$checkbox = $_POST['checkbox'];
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++) {
$product_id = $checkbox[$i];
$qty = $_POST["".$product_id.""];
$result = mysql_query("SELECT * FROM product_pc where ProductID=".$product_id);
while ($row = mysql_fetch_array($result)) {
if($product_id = isset($_POST["".$product_id.""])) {
$amount += $row['ProductPrice'] * $qty;
}
}
}
}
mysql_query("INSERT INTO `order_pc`(status, customerID, date, amount) VALUES ('In Process','1','".$date."', ".$amount.")");
$id = mysql_insert_id();
if(isset($_POST['order'])) {
$checkbox = $_POST['checkbox'];
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++) {
$product_id = $checkbox[$i];
$qty = $_POST["".$product_id.""];
mysql_query("INSERT INTO order_product_pc(orderID, productID, quantity) VALUES (".$id.",".$product_id.",".$qty.")");
}
}
header("location: ordersummarypc.php?orderid=".$id."");
?>