Phpmyadmin 仅接受来自具有多个输入值的 json 数组中的一个条目
Posted
技术标签:
【中文标题】Phpmyadmin 仅接受来自具有多个输入值的 json 数组中的一个条目【英文标题】:Phpmyadmin accepting only one entry from json array having multiple input values 【发布时间】:2020-06-30 07:02:06 【问题描述】:谁能帮我设置正确的foreach循环以将多个输入值发送到数据库(mysql),每当我在邮递员中执行此代码时,只有一个条目会上传到数据库中。我想一次发送多个“user_id”。
$input = json_decode(file_get_contents('php://input', true));
foreach($input->users as $user)
$groupId = $user->group_id;
$groups = Group::where("group_id", $groupId)
->get()->first();
foreach($input->users as $user2)
$groupMembers = new GroupMembers();
$userId = $user2->user_id;
$groupMembers->group_id = $groups->group_id;
$users1 = Users::where("user_id", $userId)
->get()->first();
$groupMembers->member_user_id = $users1->user_id;
$response = json_decode($groupMembers, true);
$save = $groupMembers->save();
输入json:
"users" : [
"group_id" : "1",
"user_id" : ["2", "3", "4"]
]
【问题讨论】:
【参考方案1】:试试下面的
foreach($input->users as $user)
$groupId = $user->group_id;
$groups = Group::where("group_id", $groupId)
->get()->first();
foreach($user->user_id as $userId)
$groupMembers = new GroupMembers();
$groupMembers->group_id = $groups->group_id;
$users1 = Users::where("user_id", $userId)
->get()->first();
$groupMembers->member_user_id = $userId;
$response = json_decode($groupMembers, true);
$save = $groupMembers->save();
【讨论】:
Postman 抛出异常:ErrorException (E_NOTICE) Trying to get property 'user_id' of non-object 请检查我的输入 json 并建议相应的编辑。【参考方案2】:试试这个你正在迭代users
它应该是user_id
foreach($input->users['user_id'] as $user)
【讨论】:
Postman 抛出异常:ErrorException (E_NOTICE) 未定义属性:stdClass::$user_id以上是关于Phpmyadmin 仅接受来自具有多个输入值的 json 数组中的一个条目的主要内容,如果未能解决你的问题,请参考以下文章