多个数组的插入在空值上失败

时间:2014-11-30 16:21:03

标签: php mysql arrays

我从动态表单中将几个数组插入到我的mysql表中。我唯一的问题是,当我将一个注释输入留空时,它不会从comments数组中插入任何更多的注释,但它会继续插入其余的其他数组。我不太确定如何处理它。正在考虑一个if声明..

编辑: 我调整了代码以反映我的实际情况。我现在知道了,因为除了注释数组之外,我的所有数组都是相同数量的值。如何在注释与cardid匹配的位置插入注释?

$ cardid array

Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 2 [6] => 2 [7] => 2 [8] => 2 [9] => 2 [10] => 3 [11] => 3 [12] => 3 [13] => 3 [14] => 3 [15] => 4 [16] => 4 [17] => 4 [18] => 4 [19] => 4 [20] => 5 [21] => 5 [22] => 5 [23] => 5 [24] => 5 [25] => 6 [26] => 6 [27] => 6 [28] => 6 [29] => 6 [30] => 7 [31] => 7 [32] => 7 [33] => 7 [34] => 7 [35] => 8 [36] => 8 [37] => 8 [38] => 8 [39] => 8 [40] => 9 [41] => 9 [42] => 9 [43] => 9 [44] => 9 [45] => 10 [46] => 10 [47] => 10 [48] => 10 [49] => 10 [50] => 11 [51] => 11 [52] => 11 [53] => 11 [54] => 11 [55] => 12 [56] => 12 [57] => 12 [58] => 12 [59] => 12 ) 

$ comment array

Array ( [0] => test1 [1] => [2] => test3 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => ) 

这是我的循环。

    $comment =  $_POST['comment'];

    for ($i=0; $i < count($_POST['cardid']); $i++ ) {

    $card_id = $_POST['cardid'][$i];
    $card_type = $_POST['cardtype'][$i];
    $top_y = $_POST['top_y'][$i];
    $left_x = $_POST['left_x'][$i];
    $pest_count = $_POST['pestcount'][$i];
    $pest_name = $_POST['pestname'][$i];

  // I tried grabing it like this

    foreach($card_id as $key=>$value){
if(in_array($key, $_POST['comment'])) {
    $comment = $value ;
}
}

     if($pest_count <> ''){

        $sql ="INSERT INTO pest_table (pest_name,pest_count,card_id,card_type,top_y,left_x,comments)
        VALUES (:pest_name,:pest_count,:card_id,:card_type,:top_y,:left_x,:comments)"; 
        $q = $pdo->prepare($sql);     
$q->execute(array(':pest_name'=>$pest_name,':pest_count'=>$pest_count,':card_id'=>$card_id,':card_type'=>$card_type,':top_y'=>$top_y,':left_x'=>$left_x,':comments'=>$comment)); 

         }

      }  

1 个答案:

答案 0 :(得分:1)

for ($i = 0; $i < count($_POST['cardid']); $i++) {
    $card_id = $_POST['cardid'][$i];
    $card_type = $_POST['cardtype'][$i];
    $top_y = $_POST['top_y'][$i];
    $left_x = $_POST['left_x'][$i];
    $pest_count = $_POST['pestcount'][$i];
    $pest_name = $_POST['pestname'][$i];

    $commentKey = $card_id-1;
    $comment = $_POST['comment'][$commentKey];

    if (!empty($pest_count)) {
        $sql ="INSERT INTO `pest_table` (`pest_name`, `pest_count`, `card_id`, `card_type`, `top_y`, `left_x`, `comments`) VALUES (:pest_name, :pest_count, :card_id, :card_type, :top_y, :left_x, :comments)";
        $q = $pdo->prepare($sql);

        // All data passed into execute() function are treated as PDO::PARAM_STR! It's better to use PDO's bindParam() or bindValue().
        $q->execute(array(
            ':pest_name' => $pest_name,
            ':pest_count' => $pest_count,
            ':card_id' => $card_id,
            ':card_type' => $card_type,
            ':top_y' => $top_y,
            ':left_x' => $left_x,
            ':comments' => $comment
        ));
    }
}

你的card_id是从1到N的索引。但是注释从0开始索引。 要通过card_id获取评论,您需要使用$comment = $_POST['comment'][$commentKey]; $commentKey = $card_id-1;