0
  • This is the model:
public function saveShifts($data) { 

        $this->db->db_debug = FALSE;
        $error = NULL;
        if (!$this->db->insert('shifts', $data)) {
            $error = $this->db->error();
        }
        return $error; 
}
  • This is the controller:
public function saveShifts(){

            $data = array (
                'user_id' => $_SESSION['id'],
                'day' => $this->input->post('day'),
                'time' => $this->input->post('time'),
                );

        $this->Shifts_model->saveShifts($data);
}
  • This is the view:

I can't post it cause it disappears when I post it.

So the view contains a form with radio buttons selection for each day of the week. You can choose either morning or evening.

MySql DB structure is: when the keys are user_id and day

+---------+---------+--------+

| day      | time    | user_id|

+---------+---------+--------+

| Sunday  | morning | 1      |

+---------+---------+--------+

| Monday  | evening | 1      |

+---------+---------+--------+

Well, I have a problem inserting multiple rows to DB using one query. In the view, the form contains radio buttons for each user to choose from for each day. It is only inserting one row (that last one) every time.

2
  • View code is required to give you a suggestion. But this question may help you : stackoverflow.com/q/6152436/12731030 Commented Apr 18, 2020 at 16:57
  • I tried this way and it didn't work. I'll try to post again the form Commented Apr 18, 2020 at 17:11

1 Answer 1

0

First of all you should update your view form for supporting values for each day. You should name your radio buttons day[] and time[].

Also for inserting multiple rows in one query, you can use CodeIgniter insert_bacth method. By using this method your controller and model will look like below:

  • model
public function saveShifts($data) { 
    return $this->db->insert_batch('shifts', $data);
}
  • controller
public function saveShifts() {
    $data = array();

    $days = $this->input->post('day[]');
    $times = $this->input->post('time[]');

    foreach ($days as $key => $day) {
        $data []  = array(
            'user_id' => $_SESSION['id'],
            'day'     => $day,
            'time'    => $times[$key],
        );
    }

    $this->Shifts_model->saveShifts($data);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. I still have a problem. My form contains 7 input tags for each day ( Sunday - Saturday) and I'm using type=radio. So since they all have the same name (day and time) as declared at the controller it reads only one of the days and all 7 of them I mean it adds only one row at the end.
Did you test renaming inputs as I said in the answer?
Yes, Thank you it worked. Another question, when I tried to display $data in my view, what is the right syntax for example for $data['username'] wasn't working, maybe $data[I]['username'] with a foreach loop?
It depends on your data structure which you send to view. for detailed explanation please read here.
Can you please check my view code, cause it inserts only the first row.
|

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.