从复选框输入PHP表单插入多个项目到一个ID MySQL [英] Insert multiple items into one ID MySQL from checkbox input PHP form

查看:154
本文介绍了从复选框输入PHP表单插入多个项目到一个ID MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是订单ID。这将是所有问题的标题。



这将插入像这样:



  if(isset($ _ POST ['Submit'])){
try {
$ orderNo = $ _SESSION ['orderNo'];
$ serviceTitle = $ _ POST ['serviceTitle'];
$ price = $ _POST ['price'];
$ quantity = $ _POST ['quantity'];
$ amount = $ _POST ['amount'];
$ b $ for($ i = 0; $ i< count($ serviceTitle); $ i ++){
$ statement = $ db-> prepare(INSERT INTO invoice(orderNo,productName ,价格,数量,金额)VALUES(?,?,?,?,?)); ($ orderNo,$ serviceTitle [$ i],$ price [$ i],$ quantity [$ i],$ amount [$ i]));
$ statement->
}

header(location:order_confirm_tech_step1.php);

catch(Exception $ e){
$ error_message = $ e-> getMessage();






$ p < serviceTitle []。
在此先感谢。

解决方案

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

  if(isset($ _ POST ['Submit'])){
try {
$ orderNo = $ _SESSION ['订单号'];
$ serviceTitle = $ _ POST ['serviceTitle'];
$ price = $ _POST ['price'];
$ quantity = $ _POST ['quantity'];
$ amount = $ _POST ['amount'];
$ b $($ i = 0; $ i if(!empty($ _ POST ['checkbox'] [$ i])) {
$ statement = $ db-> prepare(INSERT INTO invoice(orderNo,productName,price,quantity,amount)VALUES(?,?,?,?,?)); ($ orderNo,$ serviceTitle [$ i],$ price [$ i],$ quantity [$ i],$ amount [$ i]));
$ statement->
}
}

header(location:order_confirm_tech_step1.php);

catch(Exception $ e){
$ error_message = $ e-> getMessage();


code
$ b注意:name =checkbox [] p>

I badly needed this solution. Here is the image what I will checked:

Here is a Order Id. it will common to all problem Title.

Which will insert like that:

Here is the code I use:

  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();
    }
}

and every array input I use like: name="serviceTitle[]". Thanks in Advance.

解决方案

why not you try this, check if checkbox is checked before executing query

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();
    }
}

Note : name="checkbox[]"

这篇关于从复选框输入PHP表单插入多个项目到一个ID MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆