1

hello m having trouble here it always display error don't know why please help me i want to insert same only difference in user_id and section_id like this

from_id = 1 , assigned_for = 8 , user_id = 2, section_id = 1, month= january
from_id = 1 , assigned_for = 8 , user_id = 3, section_id = 3, month= january
from_id = 1 , assigned_for = 8 , user_id = 4, section_id = 4, month= january
from_id = 1 , assigned_for = 8 , user_id = 5, section_id = 5, month= january

In view file

Dropdown for months. Dropdown for select employee. heading Allow permissions along with only names sections with checkbox along with dropdown of names comming from heading Allow permissions.

Controller function

 $from = $this->_user['user_id'];
            if ($this->form_validation->run() == TRUE) {
            if ($select_section_ids) {
                foreach ($select_section_ids as $id) {$data = array(
                                'from_id' => $from,
                                'assigned_for' =>$this->input->post('assigned_for') ,
                                'user_id' => $this->input->post('user_names'),
                                'section_id' => $id,
                                'month' => $this->input->post('month'),
                            );

            $this->my_model->insert($data);
}}

'from_id' - is the loggin id 'assigned_for' - comming from dropdown of select employee 'user_id' - comming from dropdown of name from allow permissions 'section_id' - comming from checkbox 'month'- from dropdown

how to insert this m having error: Column 'user_id' cannot be null

10
  • check your input name attribuite is it correct for $this->input->post('user_names'), this name is fine user_names? Commented Sep 20, 2016 at 7:27
  • also use isset() for input values either set or not Commented Sep 20, 2016 at 7:29
  • it always display Column 'user_id' cannot be null before this i used $this->input->post('names'), but still same error Commented Sep 20, 2016 at 7:32
  • what is the name of your input field? names ? or user_names ? and it can't be null, u always having user id right? Commented Sep 20, 2016 at 7:32
  • yes it displaying null (from_id, assigned_for, user_id, section_id, month) VALUES ('1', '9', NULL, '1', 'September') because it is an single insert i want it if their are two section and two user id then insert two rows Commented Sep 20, 2016 at 7:35

1 Answer 1

1

You are using wrong index name for user id, team_lead is an array and equal to section_ids so you can use $key of section_ids for getting correct team_leads.

Example:

<?php
foreach ($select_section_ids as $key => $id) {
  $data = array(
    'from_id' => $from,
    'assigned_for' => (isset($_POST['assigned_for']) ? $_POST['assigned_for'] : 0),
    'user_id' => $_POST['team_leads'][$key], // this will use the key of section_ids, which is equal to team_leads.
    'section_id' => $id,
    'month' => $_POST['month'],
);
?>

Also note that, if team_leads is not equal to section_ids, than you must need to add check like that:

'user_id' => isset($_POST['team_leads'][$key]) ? $_POST['team_leads'][$key] : 0,
Sign up to request clarification or add additional context in comments.

5 Comments

glad to help u, @Ghugu
'assigned_for' => (isset($_POST['assigned_for']) ? $_POST['assigned_for'] : 0), 'user_id' => $_POST['team_leads'][$key] please explain me how they work
$_POST['assigned_for'] if this value is empty or not set , u can store 0 value of ignore this one , u can use as u are using before... @Ghugu
@Ghugu: for $_POST['team_leads'][$key] if you note team_leads and section_ids having same no of indexes, you can use section_ids key for getting team_leads
@Ghugu: i suggest u to read ARRAY and associative arrays, keys, multi dimensional array.

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.