插入多个数组在空值时失败


insert of multiple arrays fails on empty values

我正在从动态表单向我的 mysql 表插入几个数组。我遇到的唯一问题是,当我将一个注释输入留空时,它不会再插入注释数组中的任何注释,但它会继续插入其他数组的其余部分。我不太确定如何处理它。正在思考一个如果陈述..

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

$cardid数组

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

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]; where $commentKey = $card_id-1;