Php 如何在Laravel 5.2中使用准备好的语句
我在Laravel5.2中创建了一个简单的insert语句,如下所示Php 如何在Laravel 5.2中使用准备好的语句,php,mysql,laravel-5.2,Php,Mysql,Laravel 5.2,我在Laravel5.2中创建了一个简单的insert语句,如下所示 User::create([ 'email' => $request->input('email'), 'username' => $request->input('username'), 'password' => bcrypt($request->input('password')), ]); 但我希望我的数据库更安全,所以我更喜
User::create([
'email' => $request->input('email'),
'username' => $request->input('username'),
'password' => bcrypt($request->input('password')),
]);
但我希望我的数据库更安全,所以我更喜欢使用Prepared语句。我要求正确地编写语法?如何在语句中正确地散列我的密码
User::insert('insert into users (email, username, password) values (?,?,?)');
Eloquent在幕后为您做到这一点,在完成了所有精彩的工作之后,最终,Eloquent调用了
PDO::prepare()
,PDO::bindValue()
,最后是PDO::execute()代码>
浏览illumb\Database\Connection
的代码,以了解其要点
您需要注意的唯一一件事是不要将DB::raw()
与直接提供的用户输入一起使用,例如:
// This is bad
DB::raw('SELECT * FROM table_name WHERE col = '.$request->query('col'));
// This is good
DB::raw('SELECT * FROM table_name WHERE col = ?', [$request->query('col')]);
//or
DB::raw('SELECT * FROM table_name WHERE col = :col', ['col' => $request->query('col')]);
Eloquent在幕后为您做到这一点,在完成了所有精彩的工作之后,最终,Eloquent调用了PDO::prepare()
,PDO::bindValue()
,最后是PDO::execute()代码>
浏览illumb\Database\Connection
的代码,以了解其要点
您需要注意的唯一一件事是不要将DB::raw()
与直接提供的用户输入一起使用,例如:
// This is bad
DB::raw('SELECT * FROM table_name WHERE col = '.$request->query('col'));
// This is good
DB::raw('SELECT * FROM table_name WHERE col = ?', [$request->query('col')]);
//or
DB::raw('SELECT * FROM table_name WHERE col = :col', ['col' => $request->query('col')]);
雄辩的ORM负责处理这些安全问题。在我的第一句话中使用hash::make()
@Daan这个已经安全了吗?是的@francisunox只把这个'password'=>bcrypt($request->input('password'))
改成'password'=>hash::make($request->input('password'))
@Daan这两者之间有什么区别('password'))
与'password'=>bcrypt($request->input('password'))
?虽然我的第一条语句已经对我的密码进行了哈希运算,但bcrypt('password')
和hash::make('password'))
根据文档可以:。选择您喜欢的。雄辩的ORM会处理这些安全问题。要在我的第一句话中使用hash::make()
@Daan,这已经安全了吗?是@francisunox仅更改此'password'=>bcrypt($request->input('password'))
to'password'=>Hash::make($request->input('password'))
@Daan'password'=>Hash::make($request->input('password'))
与'password'=>bcrypt($request->input('password'))
?虽然我的第一条语句已经对我的密码进行了Hash,但这两条语句都是bcrypt('password'))
和Hash::make('password'))
根据文档可以:。选择您喜欢的。我想您指的是DB::select
而不是DB::raw
,后者返回一个SQL表达式,没有第二个绑定参数,但前者执行语句。其他一切都很好!我想您指的是DB::select
ra除了DB::raw
,后者返回一个SQL表达式,并且没有第二个绑定参数,但是前者执行语句。其他一切都很好!