$sql = "select * from users where username = '$username' and password = '$password'";
對於這種sql,對應一個萬能密碼和用戶名:php
$sql = select * from users where username = '$username' and password = ' xx' or 1='1'
不解釋,瞬間破解mysql
一樣,上面是在sql語句中查詢字段後的輸入值加了單引號,有些時候尤爲是初級程序員常常對變量不加單引號:程序員
$sql = "select * from users where username = $username and password = $password";
這時mysql解釋會被當作數字型字段來匹配sql
select * from users where username = 11 union select * from users/* and password = 54
$keyword = addslashes($keyword); $keword = str_replace("%","\$",$kwyword);
insert into users(username,password,grade) values('frank','123456','1');
insert into users(username,password,grade) values('frank','123456','3')/*,'1';
$sql = "select * from users where username=? and password=?"; //建立一個pdo對象 $mypdo = new PDO("mysql:host=localhost;port=3306;dbname=xx","root","123456"); //設置編碼 $mypdo->exec("set names utf8"); //預處理$sql $pdostatement = $mypdo->prepare(%sql); //將用戶名和密碼填入sql $pdostatement->execute(array($username,$password)); //獲得查詢結果 $result = $pdostatement->fetch(); if(empty($result)) ...