如何遍历数组元素以创建多个插入查询

时间:2013-06-20 15:20:53

标签: php arrays insert foreach

我有一个$ _POST数组,目前采用以下形式:

 ["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" 
    } 

我目前正在这样循环......

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

哪个几乎有效!它产生了这个......

"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')"

如何修改循环以生成单独的插入,每行传递一次。

2 个答案:

答案 0 :(得分:1)

必须是这样的:

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++;
    }
}

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

在准备语句中,如果需要整数,则使用PDO::PARAM_INT,并且您将使用PDO::PARAM_STR作为字符串。如果您不确定它是整数还是字符串,最好使用PDO::PARAM_STR

答案 1 :(得分:0)

我最终使用以下代码得到它,它可能是最大的hacky并且我打算在将来改进它但它可以工作。

if ($_POST['OthProb']['Issue'] != '') {
       $kv = array();
       // This will help control the array index
       $i = 0;
       // count the number of records in the array
       $count = count($_POST['OthProb']['Area']);
       $table = 'tbl_customproblems';

       // Loop through each index - stop before we reach the value of count.
       for ($i=0; $i < $count; $i++) {
           // Catch the data
           foreach ($_POST['OthProb'] as $value) {
                    $kv[] = "'".$value[$i]."'";       
            }
        // Do the insert!
        $string = "INSERT INTO $table (AccountID, PropertyID, Area, Issue, Elevation, Fix, ExpectFee, Outlay, ExpctTime, Checkbox) VALUES ('$_POST[AccountID]', '$_POST[PropertyID]', ".join(", ", $kv).") ";
        $sql = $DBH->prepare($string);
        $sql->execute();
        // Unset data value to prevent retention
        unset ($kv);  
        }