JS-NL
JS-NL

Reputation: 235

Post/input dynamic fields in Laravel

I'm building a task scheme where a few users are displayes, and each user get their own tasklist in a week. A task will be displayed in a field, so a user can always change this. I'm also using dynamic forms to add or delete a task for an user, in a week. For the dynamic forms, I'm using the same name for each form.

My code looks like the following:

<form onLoad="defaultValue" role="form" method="post" action="{{ route('addTasks') }}">
    <input type="hidden" name="uid" value="{{ $user->id }}">
    <input type="hidden" name="week" value="{{$weekshow}}">
    <input type="hidden" name="year" value="{{ $jaar }}">
    <div class="inputs">
        @foreach($tasks as $task)
            @if($task->user_id == $user->id)
                <div class="col-md-9">
                    <input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" onchange="update('tasks',1,'name')" type="text">
                     /* More inputs from dynamic form will be placed here.*/
                </div>
            @endif
        @endforeach
    </div>
    <button name="submit" class="submitbutton btn btn-info submit" value="Opslaan" type="submit">Opslaan</button>
</form>

When the user add a dynamic form, it will be placed under the first inside class="col-md-9". The dynamic input is:

<input name="dynamic[]" class="form-control field" type="text">

For saving the data I use this in my controller:

$takenvdweek = $_POST['dynamic'];
    foreach($takenvdweek as $taakvdweek) {
        DB::insert('insert into tasks values (name, user_id, week, year)', array($taakvdweek, $_POST['uid'], $_POST['week'], $_POST['year']));
    }

    return redirect()->route('project')->with('message', 'Taken opgeslagen!');

But when i try, I get the following error:

Insert value list does not match column list: 1136 Column count doesn't match value count at row 1

What am I doing wrong, and is my code well usable?

Upvotes: 2

Views: 1459

Answers (1)

Iamzozo
Iamzozo

Reputation: 2358

Change your DB::insert, the syntax is not correct:

DB::insert('insert into tasks (name, user_id, week, year) values (?, ?, ?, ?)', array($taakvdweek, $_POST['uid'], $_POST['week'], $_POST['year']))

Upvotes: 1

Related Questions