如何遍历数组元素来创建多个插入查询 [英] How to loop through array elements to create multiple insert queries

查看:147
本文介绍了如何遍历数组元素来创建多个插入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [Area] =>我有一个$ _POST数组,数组(2){
[0] =>字符串(5)标题
[1] =>字符串(5)标题
}
[问题] =>数组(2){
[0] =>字符串(3)111
[1] =>字符串(7)2222222
}
[Elevation] =>数组(2){
[0] =>字符串(8)11111111
[1] =>字符串(7)2222222
}
[Fix] =>数组(2){
[0] =>字符串(8)11111111
[1] =>字符串(6)222222
}
[ExpectFee] =>数组(2){
[0] =>字符串(8)11111111
[1] =>字符串(5)22222
}
[费用] =>数组(2){
[0] =>字符串(9)111111111
[1] =>字符串(9)222222222
}
[ExpctTime] =>数组(2){
[0] =>字符串(9)111111111
[1] =>字符串(11)22222222222
}
[复选框] =>数组(2){
[0] =>字符串(12)111111111111
[1] =>字符串(11)22222222222
}

目前我正在循环浏览。如果($ _POST ['OthProb'] ['Issue']!=''){


  $ table ='tbl_customproblems'; 
$ kv = array();
$ b foreach($ _POST ['OthProb'] as $ array){
foreach($ array as $ value){
$ kv [] ='。$ value ; $'

$ b $ string =INSERT INTO $ table(AccountID,myID,Area,Issue,Elevation,Fix,ExpectFee,Outlay,ExpctTime,Checkbox)VALUES('$ _POST [AccountID] ','$ _POST [myID]',.join(,,$ kv)。);
}

} else {
$ string = $ _SERVER ['QUERY_STRING'];
}

$ sql = $ DBH-> prepare($ string);
$ sql-> execute();

这几乎可行!它产生了这个...
$ b $ pre $ INSERT INTO tbl_customproblems(AccountID,PropertyID,Area,Issue,Elevation,Fix,ExpectFee,Outlay ('81','81','标题','标题','111','2222222','11111111','2222222','11111111','222222','11111111' ,'22222','111111111','222222222','111111111','22222222222','111111111111','22222222222')

如何修改我的循环来产生单独的插入,每一行传递一个。

解决方案

它必须是这样的:
$ b pre > if($ _POST ['OthProb'] ['Issue']!=''){
$ table ='tbl_customproblems'; (:AccountID,:myID,:Area,:Issue,:Elevation,$, :Fix,:ExpectFee,:Outlay,:ExpctTime,:Checkbox)';

$ sql = $ DBH-> prepare($ string);

$ i = 0;
foreach($ _POST ['OthProb'] as $ array){

$ sql-> bindParam(':AccountID',$ _POST ['AccountID'],PDO :: PARAM_INT );
$ sql-> bindParam(':myID',$ _POST ['myID'],PDO :: PARAM_INT);
$ sql-> bindParam(':Area',$ array ['area'] [$ i],PDO :: PARAM_STR); //它也可以是PDO :: PARAM_STR

$ sql-> execute();
$ i ++;




$ b我没有绑定所有的参数,所以你必须做这个你自己,我希望你能通过这个例子得到一个准备陈述的想法。

在一个准备语句你使用 PDO :: PARAM_INT 当你想要一个整数时,你将使用 PDO :: PARAM_STR 作为字符串。当你不确定它是一个整数还是一个字符串时,最好使用 PDO :: PARAM_STR


I have a $_POST array which currently takes the following form:

 ["Area"]=> array(2) { 
    [0]=> string(5) "Title" 
    [1]=> string(5) "Title" 
    } 
 ["Issue"]=> array(2) { 
    [0]=> string(3) "111" 
    [1]=> string(7) "2222222" 
    } 
 ["Elevation"]=> array(2) { 
    [0]=> string(8) "11111111" 
    [1]=> string(7) "2222222" 
    } 
 ["Fix"]=> array(2) { 
    [0]=> string(8) "11111111" 
    [1]=> string(6) "222222" 
    } 
 ["ExpectFee"]=> array(2) { 
    [0]=> string(8) "11111111" 
    [1]=> string(5) "22222" 
    } 
 ["Outlay"]=> array(2) { 
    [0]=> string(9) "111111111" 
    [1]=> string(9) "222222222" 
    } 
 ["ExpctTime"]=> array(2) { 
    [0]=> string(9) "111111111" 
    [1]=> string(11) "22222222222" 
 } 
 ["Checkbox"]=> array(2) { 
    [0]=> string(12) "111111111111" 
    [1]=> string(11) "22222222222" 
    } 

I am currently looping through it like this...

 if ($_POST['OthProb']['Issue'] != '') {
       $table = 'tbl_customproblems';
       $kv = array();

            foreach ($_POST['OthProb'] as $array) {
                foreach ($array as $value) {
                    $kv[] = "'".$value."'";

            }
            $string = "INSERT INTO $table (AccountID, myID, Area, Issue, Elevation, Fix, ExpectFee, Outlay, ExpctTime, Checkbox) VALUES ('$_POST[AccountID]', '$_POST[myID]', ".join(", ", $kv).")";     
        }

} else {
  $string = $_SERVER['QUERY_STRING'];
}

$sql = $DBH->prepare($string);
$sql->execute();

Which almost works! It produces this...

"INSERT INTO tbl_customproblems (AccountID, PropertyID, Area, Issue, Elevation, Fix, ExpectFee, Outlay, ExpctTime, WHCheckbox) VALUES ('81', '81', 'Title', 'Title', '111', '2222222', '11111111', '2222222', '11111111', '222222', '11111111', '22222', '111111111', '222222222', '111111111', '22222222222', '111111111111', '22222222222')"

How do I amend my loop to produce seperate inserts, one for each row being passed.

解决方案

It has to be something like this:

if ($_POST['OthProb']['Issue'] != '') {
$table = 'tbl_customproblems';
$string = 'INSERT INTO $table (AccountID, myID, Area, Issue, Elevation, Fix, ExpectFee, Outlay, ExpctTime, Checkbox) VALUES (:AccountID, :myID, :Area, :Issue, :Elevation, :Fix, :ExpectFee, :Outlay, :ExpctTime, :Checkbox)';

$sql = $DBH->prepare($string);

$i = 0;
foreach ($_POST['OthProb'] as $array) {

    $sql->bindParam(':AccountID', $_POST['AccountID'], PDO::PARAM_INT);
    $sql->bindParam(':myID', $_POST['myID'], PDO::PARAM_INT);
    $sql->bindParam(':Area', $array['area'][$i], PDO::PARAM_STR); //it can also be PDO::PARAM_STR

        $sql->execute();
        $i++;
    }
}

I didn't bind all params so you have to do that your self, I hope you get the idea of a prepare statement by this example.

At a prepare statement you use PDO::PARAM_INT when you want a integer and you will be using PDO::PARAM_STR for strings. When you are not sure if it is a integer or a string you better use PDO::PARAM_STR

这篇关于如何遍历数组元素来创建多个插入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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