我正在try 将数据插入数据库.但我不知道如何在mysql数据库中插入多选复选框数据.

  • 控制器

    public function create(Request $request)
        {
            try { 
                
                $id_student = $request->get('id_student');
                   
                $consecutive = DB::select('SELECT SUM(idRecord) FROM record GROUP BY idRecord');
                $final_consecutive = sprintf("%04d", $consecutive); 
        
                foreach($request->select as $data)
                {
                    Records::create($data);
                }
    
                return back()->with('success', 'Constancia creada correctamente');
        
            } catch (\Illuminate\Database\QueryException $e) {
                $message = $e->getMessage();
                if (strpos($message, "Duplicate entry")) {
                    return back()->with('err', 'Esta constancia ya ha sido creada');
                }
                if (strpos($message, "1366 Incorrect integer value: '' for column 'idGrupo'")) {
                    return back()->with('err', 'Debe seleccionar un grupo para poder continuar');
                }
                return back()->with('err', $message);
            }
        }

  • 看法

    @foreach ($group->students as $student)
        <tr>
           <td class="align-middle text-center">
               <input class="form-control" type="text" name="nombre" value="{{$student->lastName}} {{$student->Names}}" disabled>
            </td>
    
            <td class="align-middle text-center">
                 <input class="form-control form-control-sm" type="text" name="grade" value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" placeholder="grade" disabled>
             </td>
    
    
            <form action="{{url('/Create/Records')}}" method="POST">
                <input class="form-control form-control-sm" type="hidden" name="id_student" value="{{$student->id_student}}" >
                    <td class="align-middle text-center">
                        <input id="select" type="checkbox" name="select[]">
                    </td>
        </tr>
    @endforeach
    </tbody>
    </table>
        <div class="d-flex justify-content-center">
            <button type="submit" class="btn btn-sm btn-outline-success">Save</button>
        </div>

我试图做的是使用一个 Select 复选框,然后在控制器中,我在foreach循环中将其作为数组传递,但老实说,我认为我还远没有弄清楚它...除此之外,连续数据是自动生成的,我也不知道如何将其传递给array.

顺便说一下,我发现了这个错误:

参数1传递给Illumb\Database\Eloquent\Builder::create()

records

推荐答案

您应该向模型传递数据,如下所示:

Records::create(['id_student' => $idStudent, 'consecutive' => $consecutive]);

目前,你的捐赠方式如下:

Records::create('string');

这不是传递数据的正确方式.这就是为什么会出现错误.

Php相关问答推荐

如果活动订阅者的购物车中有订阅项目,则在WooCommerce签出中显示通知

Laravel关系-为用户获取属于组织的所有团队

在冲突和几何上插入的Postgres参数化查询在PHP中不起作用

无法使用php IMAP从Gmail获取收件箱

基于自定义域的每个购物车项目的WooCommerce定制数量输入步骤

HTAccess重写一些URL,但将所有其他URL发送到另一个页面

未收到Strava WebHook事件数据

按日期向 WooCommerce 中的我的帐户订单添加过滤器

为什么从一个页面到另一个页面都找不到 JQuery,因为它们使用相同的 twig 模板?

将 PHP 版本更改为 8.1,但 WordPress 认为我们正在使用版本 7.4?

我不明白为什么我收到未定义的数组键异常错误 - Laravel 9 PHP 8

有没有办法获取触发流操作的页面的 URI?

如何在 PHP laravel 中将图像的透明背景转换为白色?

产品页面自定义复选框可对 WooCommerce 购物车小计启用百分比折扣

使用 wp_footer 挂钩添加一些 CSS 不起作用

将产品数量、名称和结帐链接添加到 WooCommerce 添加到购物车消息

使用 PHP 的 OAuth 2.0 MAC

Composer自动加载器重复了子类的类文件路径

为什么在解压缩具有混合字符串和 int 键的关联数组时出现错误消息无法使用位置参数...?

服务器发送的事件:EventSource 抛出 MIME 错误