Order items checkbox needed atleast one check to proceed the order

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."");

?>

As you already iterate through the $POST[checkbox] array when totalling the amount, can’t you increment a counter within that loop every time to see a checked checkbox, and then surround the database updates with an if/then to make sure they only execute if the count is greater than zero?

At the same time, do you need to check the status of $POST[checkbox], or is it only submitted if it’s checked? I haven’t done this with arrays myself, so I wonder whether your amount will be correct. If they’re only submitted when the checkbox is ticked, then $countCheck might be the figure you use.

Also have a look at mysqli or PDO to access the database - the old-style MySQL calls that you are using here are deprecated now.

Im gonna try for count greater than zero. I will try consider it.

for the button order, it need to check the checkbox then it will fetch the data from the database that check by user and it will insert the selected checkbox into ‘order’ table. The main problem is, even the checkbox didnt been check, it still insert proceed into database but blank data because users havent tick one of the checbox and click the button order. For now, i need coding that give nothing process when user click button order but still havent check or tick any checkbox, users need check one of the checkbox to make order button proceed.

iam new to this, for my project. big problem now i cannot post any image to this forum cause my account is new. i wish to upload the image that i have.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.