-
webmachine
Junior Member
-
Posts: 32
Threads: 12
Joined: Sep 2015
Reputation:
1
I have a checkbox with a value of 'yes' :
PHP Code: <div class="form-control"> Check the box if a signed confidentiality guideline is on file for this client. <?php $data = array( 'name' => 'guidelines_signed', 'value' => 'yes', 'checked' => (set_value('guidelines_signed') === 'yes' ? TRUE : FALSE), 'style' => 'margin-left: 20px;' ); echo form_checkbox($data, set_checkbox('guidelines_signed')); ?> </div><!-- end of .form-control -->
I made the field nullable, with a default value of 'no' - the result was NULL being inserted into the database.
So I made the field not nullable, with a default value of 'no', thinking that 'no' would be inserted automatically - the result was an error message that the field could not be NULL. Code: `guidelines_signed` varchar(3) NOT NULL DEFAULT 'no'
This is not the first time I have had a problem like this working with CodeIgniter. Is there something going on behind the scenes with the query builder that is doing this?
-
webmachine
Junior Member
-
Posts: 32
Threads: 12
Joined: Sep 2015
Reputation:
1
Here is the code from my controller:
PHP Code: $client_data = array( 'account_num' => $client_acct_num, 'title' => $this->input->post('title'), 'first_name' => $this->input->post('first_name'), 'middle_name' => $this->input->post('middle_name'), 'last_name' => $this->input->post('last_name'), 'age' => $this->input->post('age'), 'sex' => $this->input->post('sex'), 'guidelines' => $this->input->post('guidelines'), 'guidelines_signed' => $this->input->post('guidelines_signed'), 'signed_date' => $this->input->post('signed_date'), 'home_address1' => $this->input->post('home_address1'), 'home_address2' => $this->input->post('home_address2'), 'home_city' => $this->input->post('home_city'), 'home_prov' => $this->input->post('home_prov'), 'home_postal' => $this->input->post('home_postal'), 'home_phone' => $this->input->post('home_phone'), 'phone_msg' => $this->input->post('phone_msg'), 'cell_phone' => $this->input->post('cell_phone'), 'work_phone' => $this->input->post('work_phone'), 'work_ext' => $this->input->post('work_ext'), 'email' => $this->input->post('email'), 'home_mail' => $this->input->post('home_mail'), 'mail_address1' => $this->input->post('mail_address1'), 'mail_address2' => $this->input->post('mail_address2'), 'mail_city' => $this->input->post('mail_city'), 'mail_prov' => $this->input->post('mail_prov'), 'mail_postal' => $this->input->post('mail_postal'), 'contact_notes' => $this->input->post('contact_notes'), 'inquiry_type' => $this->input->post('inquiry_type'), 'other_inquiry_type' => $this->input->post('other_inquiry_type'), 'ask_notes' => $this->input->post('ask_notes'), 'visit_date' => $this->input->post('visit_date'), 'time_spent' => $this->input->post('time_spent'), 'assignment_date' => $this->input->post('assignment_date'), 'coordinator_id' => $user_id, 'referred_by' => $this->input->post('referred_by'), 'repeat_client' => $this->input->post('repeat_client'), 'repeat_notes' => $this->input->post('repeat_notes'), 'careplanner_id' => $this->input->post('careplanner_id'), 'case_status' => $this->input->post('case_status'), 'status_change_date' => $this->input->post('status_change_date'), 'church_id' => $this->user_model->get_user_church($user_id)->church_id ); if ($this->registration_model->create_client($client_data)) { $this->session->set_flashdata('client_registered', 'New client has been registered.'); redirect('coordinators'); } else { $this->session->set_flashdata('client_registration_failed', 'New client could not be registered. Please try again or contact an administrator.'); redirect('coordinators'); }
and here is the method from the model that does the insert:
PHP Code: public function create_client($client_data) { $query = $this->db->insert('clients', $client_data); return true; }
I am fairly new to CI, and I can't see how any of this will tell me exactly what value will be inserted for 'guidelines_signed'. The query translates as
PHP Code: INSERT INTO `clients` (`account_num`, `title`, `first_name`, `middle_name`, `last_name`, `age`, `sex`, `guidelines`, `guidelines_signed`, `signed_date`, `home_address1`, `home_address2`, `home_city`, `home_prov`, `home_postal`, `home_phone`, `phone_msg`, `cell_phone`, `work_phone`, `work_ext`, `email`, `home_mail`, `mail_address1`, `mail_address2`, `mail_city`, `mail_prov`, `mail_postal`, `contact_notes`, `inquiry_type`, `other_inquiry_type`, `ask_notes`, `visit_date`, `time_spent`, `assignment_date`, `coordinator_id`, `referred_by`, `repeat_client`, `repeat_notes`, `careplanner_id`, `case_status`, `status_change_date`, `church_id`) VALUES ('16-00028-ff', 'Ms', 'f', '', 'f', '17', 'Female', 'yes', NULL, '', 'f', '', 'd', 'ON', '', '', 'no', '', '', '', '', 'yes', '', '', '', 'ON', '', '', 'General Counselling', '', '', '2016-03-11', '4', '', '3', '', 'not sure', '', '', 'Active', NULL, '1')"
-
Bhavesh
Junior Member
-
Posts: 28
Threads: 4
Joined: Nov 2015
Reputation:
0
03-06-2016, 09:59 PM
(This post was last modified: 03-06-2016, 10:40 PM by Bhavesh.)
Hi
First of all you need to check checkbox - checked or not like below:
if (isset($_POST['guidelines_signed']) && $_POST['guidelines_signed'] == 'on')
{
//if checked than append value in insert array
$client_data['guidelines_signed'] = $this->input->post('guidelines_signed');
}
else
{
//if uncheked skip to add check box value
}
and also set null default value for "guidelines_signed" column in database table
-
webmachine
Junior Member
-
Posts: 32
Threads: 12
Joined: Sep 2015
Reputation:
1
(03-06-2016, 11:34 PM)Narf Wrote: You're explicitly inserting NULL ... of course that's what's going to be inserted. The default value is used only if you provide no value at all - i.e. if the field in question is not listed in the INSERT statement.
So if I test for a value
PHP Code: if (isset($_POST['guidelines_signed']) && $_POST['guidelines_signed'] == 'on') { //if checked than append value in insert array $client_data['guidelines_signed'] = $this->input->post('guidelines_signed'); }
and skip this field if the checkbox is not selected, the default value I gave it in the database will be inserted instead?
-
Bhavesh
Junior Member
-
Posts: 28
Threads: 4
Joined: Nov 2015
Reputation:
0
(03-07-2016, 04:23 PM)webmachine Wrote: (03-06-2016, 11:34 PM)Narf Wrote: You're explicitly inserting NULL ... of course that's what's going to be inserted. The default value is used only if you provide no value at all - i.e. if the field in question is not listed in the INSERT statement.
So if I test for a value
PHP Code: if (isset($_POST['guidelines_signed']) && $_POST['guidelines_signed'] == 'on') { //if checked than append value in insert array $client_data['guidelines_signed'] = $this->input->post('guidelines_signed'); }
and skip this field if the checkbox is not selected, the default value I gave it in the database will be inserted instead?
Yes
-
skunkbad
Senior Citizen
-
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
You could also modify your insert array like this:
Code: $client_data = array(
'account_num' => $client_acct_num,
'title' => $this->input->post('title'),
'first_name' => $this->input->post('first_name'),
'middle_name' => $this->input->post('middle_name'),
'last_name' => $this->input->post('last_name'),
'age' => $this->input->post('age'),
'sex' => $this->input->post('sex'),
'guidelines' => $this->input->post('guidelines'),
'guidelines_signed' => ( $this->input->post('guidelines_signed') == 'yes' ? 'yes' : 'no' ),
'signed_date' => $this->input->post('signed_date'),
'home_address1' => $this->input->post('home_address1'),
'home_address2' => $this->input->post('home_address2'),
'home_city' => $this->input->post('home_city'),
'home_prov' => $this->input->post('home_prov'),
'home_postal' => $this->input->post('home_postal'),
'home_phone' => $this->input->post('home_phone'),
'phone_msg' => $this->input->post('phone_msg'),
'cell_phone' => $this->input->post('cell_phone'),
'work_phone' => $this->input->post('work_phone'),
'work_ext' => $this->input->post('work_ext'),
'email' => $this->input->post('email'),
'home_mail' => $this->input->post('home_mail'),
'mail_address1' => $this->input->post('mail_address1'),
'mail_address2' => $this->input->post('mail_address2'),
'mail_city' => $this->input->post('mail_city'),
'mail_prov' => $this->input->post('mail_prov'),
'mail_postal' => $this->input->post('mail_postal'),
'contact_notes' => $this->input->post('contact_notes'),
'inquiry_type' => $this->input->post('inquiry_type'),
'other_inquiry_type' => $this->input->post('other_inquiry_type'),
'ask_notes' => $this->input->post('ask_notes'),
'visit_date' => $this->input->post('visit_date'),
'time_spent' => $this->input->post('time_spent'),
'assignment_date' => $this->input->post('assignment_date'),
'coordinator_id' => $user_id,
'referred_by' => $this->input->post('referred_by'),
'repeat_client' => $this->input->post('repeat_client'),
'repeat_notes' => $this->input->post('repeat_notes'),
'careplanner_id' => $this->input->post('careplanner_id'),
'case_status' => $this->input->post('case_status'),
'status_change_date' => $this->input->post('status_change_date'),
'church_id' => $this->user_model->get_user_church($user_id)->church_id
);
-
keulu
Amazing Member
-
Posts: 82
Threads: 8
Joined: Feb 2015
Reputation:
4
i write this like that for me
'guidelines_signed' => isset($this->input->post('guidelines_signed')) ? 'yes' : 'no',
because guidelines_signed is not send if it's not checked.
|