【问题标题】:How to loop through array elements to create multiple insert queries如何循环遍历数组元素以创建多个插入查询
【发布时间】:2013-06-20 15:20:52
【问题描述】:

我有一个 $_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')"

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

【问题讨论】:

  • 你误用了prepare()$_POST 值应该进入 execute()$_SERVER['QUERY_STRING'] 怎么了?这不是 SQL 查询 :)
  • 认为我是完全自学的。你能否提供一个链接来澄清一下,因为我不知道你刚刚说了什么。
  • 看看PDO:prepare() docs的例子
  • 我不得不承认,我试图复制和修改一些我已经准备好的东西,并为不同类型的数组正常工作。只编程了几个月,完全是自学成才,在我掌握所有内容之前还有一段路要走。
  • 我肯定得到了预期值,我只是不知道我在用它们做什么:P,'OthProb' 就像一个包含大量文件夹(列名)及其关联文件(值)。我传递了很多东西,所以我这样做了,因为我可以使用盒子名称对其内容执行操作。

标签: php arrays insert foreach


【解决方案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

【讨论】:

  • 感谢您解释绑定变量的工作原理,我现在对此感觉更清楚了,但该代码我没有正确循环数组,并且正在执行 8 个插入语句(一个用于array) 我只想遍历第一行,插入,然后遍历第二行,插入。
【解决方案2】:

我最终使用下面的代码得到了它,它可能是最大的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);  
        } 

【讨论】:

    猜你喜欢
    • 2018-09-09
    • 2014-12-08
    • 2022-01-20
    • 2019-07-06
    • 1970-01-01
    • 2011-03-16
    • 2010-10-24
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多