When i run csv import using codeigniter php i stuck with this mysql error
Reason for this error is that unwanted characters or symbols in the string.
This is the CodeIgniter code I use.
$this->db->where('designationName', $row[$ex_start + 3]);
$q = $this->db->get('designation');
if ($q->result()) {
$id = $q->result_array();
$designation = $id[0]['iddesignation'];
} else {
$data = array(
'designationName' => $row[$ex_start + 3], //this is the variable that get string
'designationDate' => date('Y-m-d'),
'status_idstatus' => '1'
);
$this->db->insert('designation', $data); //this is the point that error occurs
$designation = $this->db->insert_id();
}
How I avoid this kind of characters or symbols ?
you can do
$data = array(
'designationName' => preg_replace('/[^a-zA-Z0-9-_\.]/','', $row[$ex_start + 3])
'designationDate' => date('Y-m-d'),
'status_idstatus' => '1'
);
Note: This will allow plain text+numbers to save. No, any special chars will pass
Edit 01
If you need to allow some special chars (all keyboard chars)
preg_replace('/[^A-Za-z0-9_~`\/@!$.%^#&*\\()+-=]/','', $row[$ex_start + 3])
Or you can use Escaping Queries in codeigniter
$this->db->escape()
$this->db->escape_str()
$this->db->escape_like_str()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With