Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法访问多个html复选框中的值_Php_Html_Mysql_Laravel_Checkbox - Fatal编程技术网

Php 无法访问多个html复选框中的值

Php 无法访问多个html复选框中的值,php,html,mysql,laravel,checkbox,Php,Html,Mysql,Laravel,Checkbox,我试图将html复选框中的值保存到MySQL数据库表中,但我做得不对。我需要你的建议 这是我的html @foreach($sql as $sql) <div class="form-group"> <label class="control-label mb-10" for="">{{$sql->name}}</label> <div class="input-group"> <input type="

我试图将html复选框中的值保存到MySQL数据库表中,但我做得不对。我需要你的建议

这是我的html

@foreach($sql as $sql)
<div class="form-group">
    <label class="control-label mb-10" for="">{{$sql->name}}</label>
    <div class="input-group">
        <input type="hidden" name="resource[]" value="{{$sql->id}}">
        <input type="checkbox" name="resources[]" value="c">Create
        <input type="checkbox" name="resources[]" value="r">Read
        <input type="checkbox" name="resources[]" value="u">Update
        <input type="checkbox" name="resources[]" value="d">Delete
    </div>
</div>
@endforeach
这不起作用,因为它只显示一个“选中的复选框字段”。
请问我做错了什么?

请记住,您的复选框是一个数组! 为了更好地理解发送给控制器的内容,只需编写

print_r($_POST);exit();
要查看在这之后到底发生了什么,请在您的框架方法中编写它,如下所示:

foreach($_POST['resources'] as $resources){
...
}

如果您试图保存以逗号分隔的选定值,例如:如果用户选择了c和d,则您将在数据库中保存为c,d?。如果是,则可以在单行中获取所有选定值

public function store(Request $request) {
   // get all values separated by comma
   $selectedValues = implode(",", $request->input('resources'));

  // save values in database here

}

看看你的HTML代码,你似乎要在可能不止一条SQL语句上循环,以形成复选框。服务器将无法区分这些。您应该更改复选框名称,使其更像:

<input type="checkbox" name="resources[{{$sql->id}}][]" value="c">Create
<input type="checkbox" name="resources[{{$sql->id}}][]" value="r">Read
每个SQL语句都将位于由SQL id键控的$options数组中。数组值将是由逗号分隔的复选框值

print_r($options)

[
    1 => "c,r,u,d",
    2 => "c,r,d"
]

例如,数组的名称是
resources
,您可能还应该以
$request->input('resources')
的形式访问它。是否要保存以逗号(,)分隔的选定选项。如果选择了c和d,数据库中的ex:c和d?
foreach ($request->input('resources') as $id => $resources) {
    $options[$id] = implode(',', $resources);
}
print_r($options)

[
    1 => "c,r,u,d",
    2 => "c,r,d"
]