CodeIgniter Forums

Full Version: Values in a function getting Nullified when calling SQL insert query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
@vinugenie,

You should use query binding ( https://www.codeigniter.com/user_guide/d...y-bindings ) and batch inserts [$this->db->insert_batch()]( https://www.codeigniter.com/user_guide/d...rting-data ). If you use these features then CI will automatically escape all values for you.
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