如何使用PHP和Mysql按照Json对象值向表中插入数据
我需要一个人帮忙。我需要使用PHP和MySQL根据一些Json对象值将多个数据插入表中。我在下面解释我的代码如何使用PHP和Mysql按照Json对象值向表中插入数据,php,mysql,arrays,Php,Mysql,Arrays,我需要一个人帮忙。我需要使用PHP和MySQL根据一些Json对象值将多个数据插入表中。我在下面解释我的代码 $commnt=[{ 'day_id':2, 'comment':'vodka1' },{ 'day_id':3, 'comment':'vodka2' } ] $result=[{ 'day_id':1, 'restaurant':'193' },{ 'day_id':2, 'restaurant':'193' },{ 'day_id'
$commnt=[{
'day_id':2,
'comment':'vodka1'
},{
'day_id':3,
'comment':'vodka2'
}
]
$result=[{
'day_id':1,
'restaurant':'193'
},{
'day_id':2,
'restaurant':'193'
},{
'day_id':3,
'restaurant':'193'
}
]
在这里,我需要根据day_id将来自两个Json对象的所有数据输入下表。我正在解释下表的列
db_详细信息:
我的要求是,当第天id相同时,相应的注释字段值将以其他方式输入表中,注释字段值将保持空白。预期输出如下所示
id day_id restaurant comment
1 1 193
2 2 193 vodka1
3 3 193 vodka3
$insertintodetails=mysqli_query($connect,'INSERT INTO db_details
(day_id,restaurant,comment) values ("'. $result.'","'.$result[$i]['restaurant'].'","'.$commnt[$i]['comment'].'")');
我的问题如下
id day_id restaurant comment
1 1 193
2 2 193 vodka1
3 3 193 vodka3
$insertintodetails=mysqli_query($connect,'INSERT INTO db_details
(day_id,restaurant,comment) values ("'. $result.'","'.$result[$i]['restaurant'].'","'.$commnt[$i]['comment'].'")');
这里可能有很多用例,比如两个Json对象长度可能相同或不同,但是注释应该按照day_id插入,否则它将保持空白。在我的查询中,我无法按要求插入。请帮我解决这个问题 我从您的问题细节上面创建了一个示例。您可以尝试下面的代码。它应该对你有用
$newArray = $insertintodetails = array();
foreach($result as $rs)
{
$newArray[$rs->day_id] = [
"`day_id`=>'{$rs->day_id}'",
"`restaurant`=>'{$rs->restaurant}'"
];
}
foreach($commnt as $rs){
$newArray[$rs->day_id][] = "`comment`='{$rs->comment}'";
}
foreach($newArray as $rs){
$insertintodetails[]=mysqli_query($connect,'INSERT INTO db_details SET '.implode(',',$rs));
}
//For example taken array and convert it to JSON
$comment = json_encode(array(array('day_id' => '2', 'comment' => 'vodka1'), array('day_id' => '3', 'comment' => 'vodka2')));
$result = json_encode(array(array('day_id' => '1', 'restaurant' => '193'), array('day_id' => '2', 'restaurant' => '193'), array('day_id' => '3', 'restaurant' => '193')));
//Convert JSON to array...
$arrComment = json_decode($comment, true);
$arrResult = json_decode($result, true);
foreach($arrResult AS $keyResult => $dataResult){
$day_id = $dataResult['day_id'];//day_id
$restaurant = $dataResult['restaurant'];//rasturant
$strComment = '';//comment
//Check and extract comment value from multi dimensional comment($arrComment) array...
if($getComment = find_comment_with_dayid($arrComment, $day_id)){
$strComment = $getComment;
}
//Insert records...
$insertintodetails=mysqli_query($connect,'INSERT INTO db_details
(day_id, restaurant, comment) values ("'. $day_id .'","'. $restaurant .'","'. $strComment .'")');
}
//Function will return comment for matched day_id
function find_comment_with_dayid($arrComment, $fieldKey) {
foreach($arrComment as $indCommenr => $dataComment) {
//Check for day_id, matched then return comment...
if($dataComment['day_id'] == $fieldKey) return $dataComment['comment'];
}
return FALSE;
}
在这里,我以您的问题中报告的避免JSON问题的示例数组为例。希望这个工作顺利 $commnt不是数组,它是json对象。您的数组在哪里?对不起,请检查我更新的帖子。它仍然是json。您可以使用json_解码到array的某些转换。我将测试并让您知道。@hackout:在您的代码中,某些foreach缺少{}。它无法正常工作。请检查您的代码。会出现许多错误。不,它工作正常。{day_id:2,restaurant:193,comment:}的注释值为空。这可以通过将行合并到一个语句(而不是每行一个insert语句)来大大改进。虽然它可能对小数据集没有多大影响,但随着应用程序的扩展,它可能会非常有益。所以现在,-1,但如果你编辑你的帖子,我会重新考虑。有关更多信息,请参阅