CodeIgniter Forums
Values in a function getting Nullified when calling SQL insert query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Values in a function getting Nullified when calling SQL insert query (/showthread.php?tid=72895)



Values in a function getting Nullified when calling SQL insert query - vinugenie - 02-26-2019

Hi Team
i am running into a weird problem. I have created a function to insert some data into the database. i am getting the values to be inserted from Angular. unless the insert or query function is called ($this->db->insert('table_name', $data_array), i can see all the values coming to the function correctly and i can also print those values but as soon as i call the insert / query function and i run the code, the values are showing NULL or 0. Could somebody please help me on this issue.

Code:
public function course_landing_page_data($courseTitle, $courseSubTitle, $ins_id) {


$data = array(
'course_title'      => $courseTitle,
'course_sub_title'      => $courseSubTitle,
'ins_id'   => $ins_id
);

print_r($data);



$sql = "INSERT INTO temp_courses (course_title, course_sub_title, ins_id) VALUES (".$this->db->escape($courseTitle).", ".$this->db->escape($courseSubTitle).", ".$this->db->escape($ins_id).")";
echo $sql;
$this->db->query($sql); <<<<<<<<<
$this->db->_error_message();
return true;

// }

}

appreciate your help


RE: Values in a function getting Nullified when calling SQL insert query - php_rocs - 02-26-2019

@vinugenie,

You should use query binding ( https://www.codeigniter.com/user_guide/database/queries.html?highlight=query%20binding#query-bindings ) and batch inserts [$this->db->insert_batch()]( https://www.codeigniter.com/user_guide/database/query_builder.html?highlight=batch%20query#inserting-data ). If you use these features then CI will automatically escape all values for you.


RE: Values in a function getting Nullified when calling SQL insert query - php_rocs - 02-26-2019

PHP Code:
EXAMPLE:
$sql "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3'live''Rick'));


EXAMPLE:
$data = [
 
         [
 
               'title' => 'My title',
 
               'name' => 'My Name',
 
               'date' => 'My date'
 
         ],
 
         [
 
               'title' => 'Another title',
 
               'name' => 'Another Name',
 
               'date' => 'Another date'
 
         ]
];

$this->db->insert_batch('mytable'$data); 
@vinugenie