2017-08-01 220 views
-2

我正在使用Laravel 5.4。我有一个表单,可以从用户那里获取一些信息。表单变量直接插入到数据库中。我想确保确定不会输入任何可能会损害数据库的内容。我听说过SQL注入,但我不太了解它。如何防止Laravel 5.4中的SQL注入

这是我的功能。

public function insert_data(Request $request) 
{ 
    $company_details_data = ['job_id'   => $maxID, 
           'company_id'  => $company_id, 
           'job_title'  => ucwords($request>input('job_title')), 
           'vacancy_no' => $request->input('vacancy_no'), 
           'category_id' => $request->input('category_id'), 
           'job_type_id'  => $request->input('job_type_id'), 
           'city_id'   => $request->input('city_id'), 
           'travel_required' => $request->input('travel_required'), 
           'other_locations' => ucwords($request->input('other_locations')), 
           'no_vacancy'  => $request->input('no_vacancy'), 
           'job_salary'  => $request->input('job_salary'), 
           'date_expiry'  => $request->input('date_expiry'), 
           'job_details'  => $request->input('job_details'), 
           'date_posted'  => date('Y-m-d'), 
           'qualification_required' => $request->input('qualification_required'), 
           'experience_required' => $request->input('experience_required'), 
           'skills_required'  => $request->input('skills_required'), 
           'apply_guidance'   => $request->input('apply_guidance'), 
           'duty_responsibilities' => $request->input('duty_responsibilities') 
          ]; 

    General_model::createrecord($company_details_data,'job_details'); 
} 

这是在我的模型的createrecord()函数:

public static function createrecord($data,$tbl) 
    {  
     return DB::table($tbl)->insert($data); 
    } 

我想在这里使用的htmlspecialchars但我在我的形式使用丰富的文本编辑。如果我使用htmlspecialchars,它也会改变无害的标签,如< p>,< br>等。请帮助

+0

什么是'createrecord()'方法吗?看起来你试图复制很多Eloquent实际上会为你做的事情。我建议你查看[Eloquent Documentation](https://laravel.com/docs/5.4/queries) – jfadich

+0

这是createrecord函数:return DB :: table($ tbl) - > insert($ data); –

+0

您应该使用Eloquent并为每个表创建一个模型。然后,当您使用'create'方法时,它将使用参数化查询来防止SQL注入。 – jfadich

回答

1

无法看到模型上的这些方法将这些数据实际推送到数据库中,这很难说明。

理想情况下,您希望在将数据交给任何班级之前清理数据。此外,如果您的模型尚未使用现有的ORM,则需要确保您的模型正在使用与PDO类似的功能进行数据库交互。

请参阅以下问题的答案,以确定对数据库请求的实际处理要求。

编辑: 正如其他人指出的,它最有可能在这里使用像拉罗维尔的Eloquent这样的ORM更有意义,它为您处理了很多这样的事情。

What's the best method for sanitizing user input with PHP?

+0

我已编辑该问题。模型方法实际做到这一点:返回DB :: table($ tbl) - > insert($ data); –