0

I am having a problem passing a variable to be saved as INT in MySQL. The closest thing I could find to my issue is this question but I have already verified that I am not using single and double quotes. When I print_r($this->session->userdata('subdomain')) I see the correct subdomain id on my screen (in this case 10), but when I try passing this using $params it always save subdomain_id as 0 and I can not figure out why.

Here is Project_assignees_model:

function add_prefix_project_assignees($params)
{
    $this->db->insert('prefix_project_assignees',$params);
    return $this->db->insert_id();
}

Here is my Project controller (Specifically it is the $assignees array):

function edit($entry_id)
    {   
        $this->load->library('form_validation');

        $this->form_validation->set_rules('client_id','Client Id','required|integer');
        $this->form_validation->set_rules('title','Project Title','required|max_length[255]');
        $this->form_validation->set_rules('project_status','Project Status','required');
        $this->form_validation->set_rules('project_details','Project Details','required');
        $this->form_validation->set_rules('project_estimated_hours','Project Estimated Hours','required|max_length[255]');
        $this->form_validation->set_rules('end','Project Deadline','required');

        if($this->form_validation->run())     
        {   
            date_default_timezone_set("America/New_York");
            $date = date("Y-m-d h:i:sa");

            $params = array(
                'subdomain_id' => $this->session->userdata('subdomain'),
                'client_id' => $this->input->post('client_id'),
                'title' => $this->input->post('title'),
                'project_status' => $this->input->post('project_status'),
                'project_details' => $this->input->post('project_details'),
                'project_last_updated' => $date,
                'project_estimated_hours' => $this->input->post('project_estimated_hours'),
                'start' => $this->input->post('start'),
                'end' => $this->input->post('end'),
                'owner_id' => get_current_user_id(),
            );

            $prefix_projects_id = $this->Project_model->update_prefix_projects($entry_id,$params);

            $assignees = array(
                'subdomain_id' => $this->session->userdata('subdomain'),
                'project_id' => $entry_id,
                'user_id' => $this->input->post('assignee'),
            );

            $this->load->model('Project_assignee_model');

            $insertAssignees = array();
            for ($i = 0; $i < count($assignees['user_id']); $i++)
            {
                //Get user_id from ID
                $userID = $assignees['user_id'][$i];

                $insertAssignees[$i] = array('project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]); // $insert is ready for insert_batch

                $prefix_assignees = $this->Project_assignee_model->add_prefix_project_assignees($insertAssignees[$i]);
            }

            //redirect('project/index');
        }
        else
        {   
            $this->load->model('Client_model');
            $data['prefix_clients'] = $this->Client_model->get_all_prefix_clients();
            $data['prefix_projects'] = $this->Project_model->get_prefix_projects($entry_id);
            $data['prefix_users'] = $this->aauth->list_users();
            $this->load->model('Project_assignee_model');
            $data['prefix_assignees'] = $this->Project_assignee_model->get_prefix_project_assignees($entry_id);
            $this->load->view('project/edit',$data);
        }
    }

enter image description here

enter image description here

Why does print_r show the correct subdomain_id but when it is saved to database it saves as 0?

*******UPDATE*******

Just for reference, this works:

$assignees = array(
       'subdomain_id' => 10,
       'project_id' => $entry_id,
       'user_id' => $this->input->post('assignee'),
);

But this does not (Even though printing the userdata for subdomain gives me 10):

$assignees = array(
       'subdomain_id' => $this->session->userdata('subdomain'),
       'project_id' => $entry_id,
       'user_id' => $this->input->post('assignee'),
);
Community
  • 1
  • 1
Derek
  • 4,747
  • 7
  • 44
  • 79
  • 1
    Function is `update_prefix_project_assignees`, and you are executing `$prefix_projects_id = $this->Project_model->update_prefix_projects($entry_id,$params);`, what does `update_prefix_projects`? – cpatricio Oct 22 '16 at 22:58
  • Immediately after that I sort through the multi-select values for `$assignees`, then I loop through each assignee and add it to the db using `$prefix_assignees = $this->Project_assignee_model->add_prefix_project_assignees($insertAssignees[$i]);` which works fine if I replace `$this->session->userdata('subdomain')` with `10` for example in the `$assignees` array – Derek Oct 22 '16 at 23:01
  • and on insert you leave out `subdomain_id` at all on purpose? – Jeff Oct 22 '16 at 23:02
  • Maybe a little more clarification is due, I have a projects table which saves the project id, title, description, etc. Then I have a separate table for assigning users to projects. My controller above takes a forms input and saves it to the project table and then sorts through all of the assignees and inserts them into the project_assignee table – Derek Oct 22 '16 at 23:04
  • yes, and in the line `$insertAssignees[$i] = array('project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]);` you don't add the 'subdomain_id' – Jeff Oct 22 '16 at 23:06
  • Do you have `$this->load->library('session');`? You need that to use `$this->session` – cpatricio Oct 22 '16 at 23:08
  • Yes, if I do `print_r($this->session->userdata('subdomain'));` It shows me 10, but when I use it in my controller as the `$assignees` params it saves to the column as 0. – Derek Oct 22 '16 at 23:09
  • I'm kinda confused, if your print `$this->session->userdata('subdomain')` in controller it prints 10, but saves 0, if you change manually to 10 then it saves the right value? – cpatricio Oct 22 '16 at 23:13
  • @cpatricio yes, exactly, what would cause that? – Derek Oct 22 '16 at 23:15
  • Maybe due to wrong type, from session is string, so `intval($this->session->userdata('subdomain'))` should work. – cpatricio Oct 22 '16 at 23:22
  • embarrassingly enough, I just realized my problem and it was completely obvious after hours of trying to troubleshoot. – Derek Oct 22 '16 at 23:22

1 Answers1

1

Sometimes I guess it just takes writing it out for others before I realize my problem. When I loop through each assignee to add them to the table I realized I was not passing the subdomain_id with it.

I had this:

$insertAssignees[$i] = array('project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]); // $insert is ready for insert_batch

Needed to be changed to have subdomain_id included

$insertAssignees[$i] = array('subdomain_id' => $this->session->userdata('subdomain'), 'project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]); // $insert is ready for insert_batch
Derek
  • 4,747
  • 7
  • 44
  • 79