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

标签 php mysql arrays

我正在从一个动态表单向我的 mysql 表中插入几个数组。我遇到的唯一问题是,当我将一个评论输入留空时,它不会从评论数组中插入更多评论,但它会继续插入其他数组的其余部分。我不太确定如何处理它。正在考虑 if 语句..

编辑: 我已经调整了代码以反射(reflect)我的实际情况。我现在知道这是因为我的所有数组都是相同数量的值,除了评论数组。如何在索引与卡片匹配的位置插入评论?

$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 ) 

$评论数组

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

关于php - 多个数组的插入在空值上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215342/

相关文章:

mysql - 你好,我有一个带有 sql 数据库的 vb.net 应用程序,我想搜索数据库并使用组合框和文本框过滤搜索

php - 将 PHP "grouped"数组转换为 "segmented"数组

c++ - 从 C++ 中的函数返回二维数组

php - 插入到两个具有相关键的不同表中

php-显示来自 mysql 的数据时新行不起作用

mysql - 仅在 vb.net 中查询同一按钮

javascript - 如何使用express-validator对嵌套对象实现验证

php - 更改 PHP SoapClient 请求中的 namespace ?

php - 如何在 Laravel 中添加动态页面标题

mysql - 打开街道 map 地址解码