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.

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26

1 Answers1

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);
}
mmd kh
  • 41
  • 3
  • 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. – Victoria's Secret Apr 18 '20 at 23:09
  • Did you test renaming inputs as I said in the answer? – mmd kh Apr 19 '20 at 15:58
  • 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? – Victoria's Secret Apr 24 '20 at 22:03
  • It depends on your data structure which you send to view. for detailed explanation please read [here](https://codeigniter.com/userguide3/tutorial/news_section.html#display-the-news). – mmd kh Apr 28 '20 at 15:03
  • Can you please check my view code, cause it inserts only the first row. – Victoria's Secret May 04 '20 at 19:52
  • https://stackoverflow.com/questions/61597620/insert-multiple-rows-into-db-using-one-form Here I have my view code. Thank you – Victoria's Secret May 04 '20 at 19:59