I am inserting some data into a MySQL table using CodeIgniter. Because I am using INSERT IGNORE INTO
and do not want to edit the active records class to enable this feature, I am generating the SQL query manually.
$this->db->query("INSERT IGNORE INTO my_table(lat, lng, date, type)
VALUES ('" . $data['lat'] . "', '" . $data['lng'] . "', '" . $data['date'] . "', '" . $data['type'] . "')");
Problem: The query failed when the string in $data['type']
contained a single quote. How can I make it such that these characters that need to be escaped gets escaped automatically, like when using Active records?
It is unsafe not to use Query Binding. This will automatically escape all the values:
$sql = "INSERT IGNORE INTO my_table(lat, lng, date, type) VALUES (?,?,?,?);";
$this->db->query($sql, [$data['lat'], $data['lng'], $data['date'], $data['type']]);