0

希望有人可以建议。

inputs当for 循环内为空时,我面临一个处理错误的小问题。在这种情况下,我必须使用 for 循环,因为我要同时插入多个输入。

在检查输入是否为空时,唯一会发生的事情是循环什么都没有,只是循环并且什么都不返回。

PHP代码:

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.');
    }
}
4

0 回答 0