error inserting an array in codeigniter - php

when I insert an array in the db, I get the error "unidentified index" in some of the variables even though they all have their corresponding values. Tried using print_r, and it returned an array with values, but when I try to insert the array, the values become null. I dont understand why and I can't see the error in my code.
here's my controller
public function test()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$user_id = $session_data['user_id'];
$table_id = $this->input->post('table');
$start = $this->input->post('start');
$end = $this->input->post('end');
$query = $this->process_resto_list->test($table_id);
if ($query->num_rows() > 0)
{
$row = $query->row();
$resto_id = $row->resto_id;
$table_number= $row->table_number;
$table_size = $row->table_size;
}
//$resto_id = $session_data['user_id'];
$values = array(
'resto_id' => $resto_id,
'diner_id' => $user_id,
'date' => $this->input->post('date'),
'start_time' => $start,
'end_time' => $end,
'table_number' => $table_number,
'number_of_people' => $table_size,
);
if($this->process_resto_list->insert_reservation($values))
redirect('reservation_test/reservation', 'location');
//print_r($values);
}
Here's my model
public function insert_reservation($values)
{
date_default_timezone_set("Asia/Manila");
$data = array(
'resto_id' => $values['resto_id'],
'diner_id' => $values['user_id'],
'date' => date('Y-m-d'),
'start_time' => $values['start'],
'end_time' => $values['end'],
'table_number' => $values['table_number'],
'number_of_people' => $values['table_size'],
);
return $this->db->insert('resto_reservations', $data);
}
When using print_r here's the result of the array
Array ( [resto_id] => 9 [diner_id] => 14 [date] => [start_time] => 11:00 [end_time] => 13:00 [table_number] => 6 [number_of_people] => 4 )
Please help me point out the error. Thank you

In your model. Change values as follows
$data = array(
'resto_id' => $values['resto_id'],
'diner_id' => $values['diner_id'],
'date' => date('Y-m-d'),
'start_time' => $values['start_time'],
'end_time' => $values['end_time'],
'table_number' => $values['table_number'],
'number_of_people' => $values['number_of_people'],
);

I just replaced my model with this. Thanks everyone!
$data = array(
'resto_id' => $values['resto_id'],
'diner_id' => $values['diner_id'],
'date' => date('Y-m-d'),
'start_time' => $values['start_time'],
'end_time' => $values['end_time'],
'table_number' => $values['table_number'],
'number_of_people' => $values['number_of_people'],
);

Related

Codeigniter Model Undefined Index on Array

I'm trying to use Codeigniter to update my value once the user had submitted the form. But unfortunately, there was an error of undefined index for few of the variable which I store in an array.
Below is the Code that I use to pass from Controller to the Model
Controller Code :
$prep_data = array(
'full_name' => $this->input->post('full_name'),
'nric' => $this->input->post('nric'),
'dob' => $this->input->post('dob'),
'mobile_phone' => $this->input->post('mobile_phone'),
'email' => $this->input->post('email'),
'address_line_1' => $this->input->post('address_line_1'),
'address_line_2' => $this->input->post('address_line_2'),
'postcode' => $this->input->post('postcode'),
'state' => $this->input->post('state'),
'current_brand' => $this->input->post('current_brand'),
'pregnant' => $isPregnant, //Edit by Marcus
'breastfeeding' => $isBreastfeeding, //Added by Marcus
'child_full_name' => $this->input->post('child_full_name'),
'child_dob' => $this->input->post('child_dob'),
'child_current_brand' => $this->input->post('child_current_brand'),
'expected_give_birth_date' => $this->input->post('expected_give_birth_date'),
'image' => '',
'quantity_purchase' => $this->input->post('quantity_purchase'),
'foc_amount' => $this->input->post('foc_amount'),
'marketing_opt' => $this->input->post('marketing_opt'),
'centre_id' => get_cookie('abbott_centre', TRUE),
'user_id' => $this->session->id,
'sampling_day' => $centre_data['sampling_day'],
'created_at' => $datetime,
'updated_at' => $datetime
);
$this->entry_model->update_entry($prep_data);
Model Code :
function update_entry($data){
echo print_r($data);
if(!empty($data['full_name'])) $this->db->set('full_name', $data['full_name']);
if(!empty($data['nric'])) $this->db->set('nric', $data['nric']);
if(!empty($data['dob'])) $this->db->set('dob', $data['dob']);
if(!empty($data['mobile_phone'])) $this->db->set('mobile_phone', $data['mobile_phone']);
if(!empty($data['email'])) $this->db->set('email', $data['email']);
if(!empty($data['address_line_1'])) $this->db->set('address_line_1', $data['address_line_1']);
if(!empty($data['address_line_2'])) $this->db->set('address_line_2', $data['address_line_2']);
if(!empty($data['postcode'])) $this->db->set('postcode', $data['postcode']);
if(!empty($data['state'])) $this->db->set('state', $data['state']);
if(!empty($data['current_brand'])) $this->db->set('current_brand', $data['current_brand']);
if(!empty($data['pregnant'])){ //Fix by Marcus
$this->db->set('pregnant', $data['pregnant']);
}else {
$this->db->set('pregnant', 0);
}
if(!empty($data['breastfeeding'])){ //Fix by Marcus
$this->db->set('breastfeeding', $data['breastfeeding']);
}
else {
$this->db->set('breastfeeding', 0);
}
if(!empty($data['expected_give_birth_date'])) $this->db->set('expected_give_birth_date', $data['expected_give_birth_date']);//Fix by Marcus
if(!empty($data['child_full_name'])) $this->db->set('child_full_name', $data['child_full_name']);
if(!empty($data['child_dob'])) $this->db->set('child_dob', $data['child_dob']);
if(!empty($data['child_current_brand'])) $this->db->set('child_current_brand', $data['child_current_brand']);
if(!empty($data['image'])) $this->db->set('image', $data['image']);
if(isset($data['quantity_purchase'])) $this->db->set('quantity_purchase', $data['quantity_purchase']);
if(isset($data['foc_amount'])) $this->db->set('foc_amount', $data['foc_amount']);
if(!empty($data['marketing_opt'])) $this->db->set('marketing_opt', $data['marketing_opt']);
if(!empty($data['centre_id'])) $this->db->set('centre_id', $data['centre_id']);
if(!empty($data['user_id'])) $this->db->set('user_id', $data['user_id']);
if(!empty($data['created_at'])) $this->db->set('created_at', $data['created_at']);
if(!empty($data['updated_at'])) $this->db->set('updated_at', $data['updated_at']);
$this->db->where('id', $data['id']);
$this->db->update('entry');
}
Other $data[] value had no problem but only the 'pregnant' and 'breastfeeding' had the error on Undefined Index. I did try to print_r to show the $data value and all are seem to be alright.
Below are the output :
Array ( [full_name] => e2e [nric] => 123 [dob] => 2019-07-09 [mobile_phone] => 1231231231 [email] => [address_line_1] => [address_line_2] => [postcode] => [state] => [current_brand] => [pregnant] => 0 [breastfeeding] => 1 [child_full_name] => [child_dob] => 0000-00-00 [child_current_brand] => [expected_give_birth_date] => 2019-07-09 [image] => [quantity_purchase] => 123123 [foc_amount] => 123 [sampling_day] => [updated_at] => 2019-07-10 19:27:43 [id] => 8 ) 1Array ( [image] => 8_20190710_192743.jpg [id] => 8 ) 1
What will be the caused for this 'pregnant' and 'breastfeeding' array? As it keeps getting NULL as a return.
Thanks
You need to use isset() to check either index set or not like
$isPregnant = 0;
$isBreastfeeding = 0;
if(isset($_POST['pregnant']) && $_POST['pregnant'] == 1)
{
$isBreastfeeding = 0;
$isPregnant = 1; //Is breastfeeding selected
}
if(isset($_POST['breastfeeding']) && $_POST['breastfeeding'] == 1)
{
$isPregnant = 0;
$isBreastfeeding = 1; //Is breastfeeding selected
}

PHP MVC query to get all existing rows is returning only 1 row

I have a working query but its only returning 1 row - where or what can I do to return all existing rows?
Model:
public function getInfo() {
$info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");
if ($info_query->num_rows){
return array(
'info_id' => $info_query->row['info_id'],
'name' => $info_query->row['name'],
'amount' => $info_query->row['amount'],
'date_start' => $info_query->row['date_start'],
'date_end' => $info_query->row['date_end'],
'status' => $info_query->row['status'],
'date_added' => $info_query->row['date_added']
);
}
}
Controller:
$info_options = $this->model_extension_total_info->getInfo();
if ($info_options){
$json['info_options'] = array();
$json['info_options'][] = array(
'info_id' => $info_options['info_id'],
'name' => $info_options['name'],
'amount' => $info_options['amount'],
'date_start' => $info_options['date_start'],
'date_end' => $info_options['date_end'],
'status' => $info_options['status'],
'date_added' => $info_options['date_added']
);
}
When I try foreach() in the controller, I still only get one row.:
foreach ($info_options as $info) {
var_dump($info_options['name']);exit;
//var_dump($info_options['name']); results: Warning: Illegal string offset 'name'
}
In the model, when I dump:
$info_query I get 9 -- which is all of the rows I'm expecting.
Not particularly certain what I'm missing or doing wrong.
You're not looping over the result. Not in model nor in the controller. Though I don't know why you'd do this twice. Maybe I'm just not understanding your code.
Model:
$data = [];
if ($info_query->num_rows){
foreach ($info_query->result() as $row) {
$data[] = array(
'info_id' => $row['info_id'],
'name' => $row['name'],
'amount' => $row['amount'],
'date_start' => $row['date_start'],
'date_end' => $row['date_end'],
'status' => $row['status'],
'date_added' => $row['date_added']
);
}
return $data;
}
Controller:
$info_options = $this->model_extension_total_info->getInfo();
$json['info_options'] = array();
if ($info_options){
foreach ($info_options as $info) {
$json['info_options'][] = array(
'info_id' => $info['info_id'],
'name' => $info['name'],
'amount' => $info['amount'],
'date_start' => $info['date_start'],
'date_end' => $info['date_end'],
'status' => $info['status'],
'date_added' => $info['date_added']
);
}
}
Technically, you don't have to loop at all. You can just:
public function getInfo() {
$info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");
if ($info_query->num_rows){
return $info_query->result();
}
else
{
return false;
}
}
and
$info_options = $this->model_extension_total_info->getInfo();
// and that's it, just do whatever you need with $info_options

Saving PHP variable to MySQL INT is always 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);
}
}
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'),
);
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

Unable to insert data into database using codeigniter

I want to insert data into my database. I am using codeigniter framework to build my app.
When I click on submit button,It don't give any error just reload the same page and data in not inserted in database.
Following my code to insert data into database -
public function addSale($saleDetails = array(), $items = array(), $warehouse_id)
{
foreach($items as $data){
$product_id = $data['product_id'];
$product_quantity = $data['quantity'];
$this->updateProductQuantity($product_id, $warehouse_id, $product_quantity);
}
// sale data
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
if($this->db->insert('sales', $saleData)) {
$sale_id = $this->db->insert_id();
$addOn = array('sale_id' => $sale_id);
end($addOn);
foreach ( $items as &$var ) {
$var = array_merge($addOn, $var);
}
if($this->db->insert_batch('sale_items', $items)) {
return true;
}
}
return false;
}
Based on https://ellislab.com/codeigniter/user-guide/database/examples.html , it seems to me, that you are missing a line -> $this->db->insert('sales', $saleData); . You do have it inside an if statement, but if statement will not execute it as a code, but check if it returns true.
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
$this->db->insert('sales', $saleData);
This should work. You can also check if it succeeds like this ->
return ($this->db->affected_rows() != 1) ? false : true;

CodeIgniter: Populating two tables through two arrays from the same form?

I tried doing the following and surprisingly it only populates the first table (new_guest) while writing only zero values to the other (new_reservation). How can I properly configure this?
Controller:
function guest_checks_in() {
$data_guest = array (
'guest_title' => $this->input->post('guest_title'),
'guest_name' => $this->input->post('guest_name'),
'guest_gender' => $this->input->post('guest_gender'),
'guest_phone' => $this->input->post('guest_phone'),
'guest_email' => $this->input->post('guest_email'),
'guest_address' => $this->input->post('guest_address'),
'guest_province' => $this->input->post('guest_province'),
'guest_country' => $this->input->post('guest_country'),
'guest_postal_code' => $this->input->post('guest_postal_code'),
'guest_dob' => $this->input->post('guest_dob'),
'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl')
);
$data_reservation = array (
'room_type' => $this->input->post('room_type'),
'meal_type' => $this->input->post('meal_type'),
'bed_type' => $this->input->post('bed_type'),
'ext_beds' => $this->input->post('ext_beds'),
'number_of_guests' => $this->input->post('number_of_guests'),
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'reservation_duration' => $this->input->post('reservation_duration'),
'room_number' => $this->input->post('room_number'),
'total' => $this->input->post('total')
);
$this->reservations_model->add_guests($data_guest);
$this->reservations_model->add_reservations($data_reservation);
$this->confirm_check_in();
}
Model:
class Reservations_model extends CI_Model {
function add_guests($data_guest) {
$this->db->insert('new_guest', $data_guest);
return;
}
function add_reservations($data_reservation) {
$this->db->insert('new_reservation', $data_reservation);
return;
}
}
it will be good if you use transaction. if data in both tables must be entered.
function guest_checks_in() {
$data_guest = array (
'guest_title' => $this->input->post('guest_title'),
'guest_name' => $this->input->post('guest_name'),
'guest_gender' => $this->input->post('guest_gender'),
'guest_phone' => $this->input->post('guest_phone'),
'guest_email' => $this->input->post('guest_email'),
'guest_address' => $this->input->post('guest_address'),
'guest_province' => $this->input->post('guest_province'),
'guest_country' => $this->input->post('guest_country'),
'guest_postal_code' => $this->input->post('guest_postal_code'),
'guest_dob' => $this->input->post('guest_dob'),
'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl')
);
$data_reservation = array (
'room_type' => $this->input->post('room_type'),
'meal_type' => $this->input->post('meal_type'),
'bed_type' => $this->input->post('bed_type'),
'ext_beds' => $this->input->post('ext_beds'),
'number_of_guests' => $this->input->post('number_of_guests'),
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'reservation_duration' => $this->input->post('reservation_duration'),
'room_number' => $this->input->post('room_number'),
'total' => $this->input->post('total')
);
$this->reservations_model->add_all_guests($data_guest,$data_reservation);
$this->confirm_check_in();
}
in model
function add_all_guests($data_guest,$data_reservation) {
$this->db->trans_begin();
$this->db->insert('new_guest', $data_guest);
$this->db->insert('new_reservation', $data_reservation);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return false;
}
else
{
$this->db->trans_commit();
return true;
}
}

Categories