Hope someone can advise.
I'm facing a small issue of handling errors while the inputs
are empty inside the for loop.
I have to use for loop in this case because I'm inserting multiple inputs in the same time.
On checking if inputs are empty the only thing is happen is looping on nothing just loop and return nothing.
PHP code:
public function Add_new_order($tableno, $cashier_name, $date, $time, $name, $quantity, $price, $sub_total, $total) {
$stmt = $this->conn->prepare("SELECT MAX(order_num) AS order_number FROM all_orders");
$stmt->execute();
while ($row = $stmt->fetch()) {
$order_num = $row["order_number"];
if(is_null($order_num)) {
$order_num = '100000';
}
else {
$order_num += 1;
}
}
for($i = 0; $i < count($name); $i++) {
if($quantity[$i] > 0) {
if($name[$i] != "" && !empty($quantity[$i]) && $price[$i] != "") {
$stmt1 = $this->conn->prepare("INSERT INTO `all_orders` (`ID`, `order_num`, `tablenum`,`cashier_name`, `date`, `time`, `item_name`, `item_code`, `quantity`, `price`, `total`) VALUES ('','".$order_num."','".$tableno."','".$cashier_name."','".$date."','".$time."','".$name[$i]."','','".$quantity[$i]."','".$price[$i]."','".$sub_total[$i]."')");
$stmt1->execute();
}
} else {
$this->Error_msg('Error.');
}
}
$stmt2 = $this->conn->prepare("INSERT INTO `biling`(`ID`, `order_num`, `tablenum`,`cashier_name`, `date`, `time`, `total`) VALUES ('','".$order_num."','".$tableno."','".$cashier_name."','".$date."','".$time."','".$total."')");
$stmt2->execute();
if($stmt1 && $stmt2) {
$this->Success_msg('Done.');
} else {
$this->Error_msg('Error occurred.');
}
}