gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:06:36 25 4
gpt4 key购买 nike

我正在从一个动态表单向我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com