Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3905
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T05:43:55+00:00 2022-06-03T05:43:55+00:00

php – insert multiple data based on checkbox selection in Laravel

  • 0

[ad_1]

I’m trying to insert multiple data based on checkbox selection in Laravel but when I submit the dat it throws me this error:

Undefined index: select

  • My function that does the array insert of the selected checkboxes and request the data:
public function create(Request $request)
    {
        try {
            $data[] = request()->except('token_');
            for ($i = 0; $i < count($data['select']); $i++) {

                $consecutive = DB::select('SELECT SUM(certificationId) FROM certifications GROUP BY certificationId');
                $final_consecutive = sprintf("%04d", $consecutive);

                DB::table('certifications')->insert([
                    'student_id' => $data['student_id'][$i],
                    'consecutive' =>  $data['consecutive'][$i],
                    'year' => $data['year'][$i],
                    'season' => $data['season'][$i],
                    'program' => $data['program'][$i],
                    'course' => $data['course'][$i],
                    'level' => $data['level'][$i],

                ]);
            }
            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);
        }
    }
      <form action="{{url('/Create/Certifications/')}}" method="POST">
                                {{csrf_field()}}

                                <td class="align-middle text-center">
                                    <input value="" name="consecutive[{{$student->student_id}}]">
                                    
                                </td>

                                <td class="align-middle text-center">
                                @foreach ($group->levels as $level)
                                    <input type="hidden" value="{{ $level->course->program->season->year->year }}" id="year" name="year[{{$student->student_id}}]">
                                    {{$level->course->program->season->year->year}}
                              
                                </td>
                                <td class="align-middle text-center">
                               
                                    <input type="hidden" value="{{ $level->course->program->season->seasonName }}" name="season[{{$student->student_id}}]">
                                    {{$level->course->program->season->seasonName}}
                                
                                </td>
                                <td class="align-middle text-center">
                              
                                    <input type="hidden" value="{{ $level->course->program->programName }}" name="program[{{$student->student_id}}]">
                                    {{$level->course->program->programName}}
                              
                                </td>
                                <td class="align-middle text-center">
                                    <input type="hidden" value="{{ $level->course->courseName }}" name="course[{{$student->student_id}}]">
                                    {{$level->course->courseName}}
                                </td>
                                <td class="align-middle text-center">
                            
                                    <input type="hidden" value="{{ $level->levelName }}" name="level[{{$student->student_id}}]">
                                    {{$level->levelName}}
                              
                                </td>
                                <td class="align-middle text-center">
                               
                                    <input type="hidden" value="{{ $level->course->duration }}" name="duration[{{$student->student_id}}]">
                                    {{$level->course->duration}}
                         
                                </td>
                                <td class="align-middle text-center">
                             
                                    <input type="hidden" value="{{ $level->course->beginDate }}" name="fechaInicio[{{$student->student_id}}]">
                                    {{$level->course->beginDate}}
                               
                                </td>
                                <td class="align-middle text-center">
                           
                                    <input type="hidden" value="{{ $level->course->endDate }}" name="endDate[{{$student->student_id}}]">
                                    {{$level->course->endDate}}
                               
                                </td>
                        @endforeach
                                <input class="form-control form-control-sm" type="hidden" name="student_id[]" value="{{$student->student_id}}" >

                                <td class="align-middle text-center">
                                    <input id="select" type="checkbox" name="select[{{$student->student_id}}]">
                                </td>
                        </tr>
                        @endif
                        @endforeach
                    </tbody>
                </table>
                    <div class="d-flex justify-content-center">
                        <button type="submit" class="btn btn-sm btn-outline-success">Create</button>
                    </div>
                </form>

I also had something different in my create function. I put the index sutdent_id as index, something like this: foreach ($request->get('student_id') as $index => $student_id) but it didn’t work either, it was the same error but with index student_id

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8055 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1842 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1924 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.