我有一个包含大量字段的表单。有时,用户会在其中提供包含单引号的信息和说明。
我正在使用Jquery和CI验证数据,问题是显然ActiveRecord没有转义单引号,导致插入/更新数据时出错。
ActiveRecord是不是应该自动转义这些字符?如果没有,在用户输入中处理单引号的常用方法是什么?
处理插入的模型函数的示例代码:
public function setLicense($dataArray, $data_id="")
{
$iRows = 0; // Rows found.
$DB = $this->load->database('some_database',TRUE,TRUE);
//var_dump($dataArray);
if(empty($dataArray))
return(FALSE);
if(!empty($data_id))
{
$DB->where('idx',$data_id);
$iRows=$DB->count_all_results('some_table');
}
else
{
if(isset($LicenseData['idx']))
{
$license = $LicenseData['idx'];
$DB->where('idx',$license);
$iRows=$DB->count_all_results('some_table');
}
}
if(!$iRows)
$DB->insert('some_table',$dataArray);
else
{
$DB->where('idx',$data_id);
$DB->update('some_table',$dataArray);
}
return(TRUE);
}
答案 0 :(得分:1)
在连接ODBC数据库驱动程序时,您所描述的行为似乎是有意的。以下是this Ellislab forum discusssion的引用:
由于ODBC的本质 - 您几乎可以使用任何数据库 没有CodeIgniter的平台可以知道它是哪一个。 不同的数据库有不同的转义字符和规则,所以 这真的取决于你做这种逃避。