【问题标题】:Insert multiple items into one ID MySQL from checkbox input PHP form从复选框输入PHP表单将多个项目插入一个ID MySQL
【发布时间】:2017-02-15 02:00:07
【问题描述】:

我非常需要这个解决方案。 这是我将检查的图像:

这是一个订单 ID。它对所有问题标题都是通用的。

这样插入:

这是我使用的代码:

  if(isset($_POST['Submit'])){
    try{
    $orderNo = $_SESSION['orderNo'];
    $serviceTitle=$_POST['serviceTitle'];
    $price= $_POST['price'];    
    $quantity= $_POST['quantity'];  
    $amount= $_POST['amount'];
    
    for ($i=0; $i<count($serviceTitle); $i++){
        $statement = $db->prepare("INSERT INTO invoice (orderNo,productName,price,quantity,amount) VALUES (?,?,?,?,?)");
        $statement->execute(array($orderNo,$serviceTitle[$i],$price[$i],$quantity[$i],$amount[$i]));
    }
        
    header("location: order_confirm_tech_step1.php");
    }
    catch(Exception $e) {
            $error_message = $e->getMessage();
    }
}

我使用的每个数组输入都像:name="serviceTitle[]"。 提前致谢。

【问题讨论】:

  • 你应该养成accepting the appropriate answer的习惯(它对你有用或解决了你的问题)。它将鼓励其他开发人员进一步帮助您。
  • 另外,显示您的 HTML 表单。

标签: php mysql arrays for-loop


【解决方案1】:

为什么不试试这个,在执行查询之前检查复选框是否被选中

if(isset($_POST['Submit'])){
    try{
    $orderNo = $_SESSION['orderNo'];
    $serviceTitle=$_POST['serviceTitle'];
    $price= $_POST['price'];    
    $quantity= $_POST['quantity'];  
    $amount= $_POST['amount'];

    for ($i=0; $i<count($serviceTitle); $i++){
       if(!empty($_POST['checkbox'][$i])) {
            $statement = $db->prepare("INSERT INTO invoice (orderNo,productName,price,quantity,amount) VALUES (?,?,?,?,?)");
            $statement->execute(array($orderNo,$serviceTitle[$i],$price[$i],$quantity[$i],$amount[$i]));
        }
    }

    header("location: order_confirm_tech_step1.php");
    }
    catch(Exception $e) {
            $error_message = $e->getMessage();
    }
}

注意:name="checkbox[]"

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2020-11-07
    • 2012-08-20
    • 2011-09-06
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多