0

I am inserting multiple data into mysql database, apparently everything works fine, but when I checked the data in phpmyadmin, it seems that only the first value of the first selected checkbox is being inserted multiple times, which means only the first id_student is the value that is being inserted multiple times. I think it's a mapping problem but I don't know how to solve it, can someone help me?

  • This is my view
@foreach ($group->students as $student)
         @if($student->pivot->pass)
         <tr>
            <td class="align-middle text-center">
                <input class="form-control" type="text" name="name" value="{{$student->last_name}} {{$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">
             {{csrf_field()}}

                  <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>
        @endif
@endforeach
  • This is my function in Controller.

     public function create(Request $request)
        {
            try { 
                
                $id_student = $request->get('id_student');
                   
                $consecutive = DB::select('SELECT SUM(idRecord) FROM records GROUP BY idRecord'); 
                $final_consecutive = sprintf("%04d", $consecutive); 
                foreach($request->select as $data)
                {
                    Records::create(['id_student' => $id_student, 'consecutive' => $final_consecutive]);
                }
    
                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);
            }
        }

  • Image of the table "Record"

records

0

1 Answer 1

1

You have one form per $group->students which means only one should be inserted at a time. The fact you end up inserting many of the same is just problematic. If you truly want to insert multiple records at a time you need to wrap your @foreach in the form:

<form action="{{url('/Create/Records/')}}" method="POST">
{{csrf_field()}}
@foreach ($group->students as $student)
    @if($student->pivot->pass)
         <tr>
            <td class="align-middle text-center">
                <input class="form-control" type="text" name="name[]" value="{{$student->last_name}} {{$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>



            <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>
    @endif
@endforeach
</form>

then you can iterate on the id_student:

    public function create(Request $request)
        {
            try { 
                foreach ($request->get('id_student') as $index => $id_student) {
                    $consecutive = DB::select('SELECT SUM(idRecord) FROM records GROUP BY idRecord'); 
                    $final_consecutive = sprintf("%04d", $consecutive); 
                    Records::create(['id_student' => $id_student, 'consecutive' => $final_consecutive]);
               }
              
                return back()->with('success', 'Constancia creada correctamente');
            } catch (\Illuminate\Database\QueryException $e) {
                // ....
            }
        }


Sidenote: I'm not sure why you are adding the name, grade and select in the form if you don't actually intend to use them.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! And yes, sorry, I forgot to update the form code. I only have the select in the form, but thanks for the note (:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.