当我使用codeigniter PHP运行csv import时,我坚持使用这个mysql错误
Reason for this error is that unwanted characters or symbols in the string.
这是我使用的CodeIgniter代码.
$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();
}
我如何避免这种字符或符号?
最佳答案
你可以做
原文链接:https://www.f2er.com/mysql/432894.html$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
编辑01
如果你需要允许一些特殊的字符(所有键盘字符)
preg_replace('/[^A-Za-z0-9_~`\/@!$.%^#&*\\()+-=]/',$row[$ex_start + 3])
或者你可以使用Escaping Queries in codeigniter
> $this-> db-> escape()
> $this-> db-> escape_str()
> $this-> db-> escape_like_str()