An entry-level example
| The code is as follows |
Copy Code |
|
$k = $_request[' K '];
$k = Addslashes ($k);//Escape: Single quotes, double quotes, backslash, NULL
$k = str_replace ('% ', ' \% ', $k);
$k = Str_replace (' _ ', ' \_ ', $k);
$sql = "The select * from users where name is '% $k% '";
if (!empty ($k)) {
$res = mysql_query ($sql, $con) or Die (Mysql_error ());
if ($row = Mysql_fetch_assoc ($res)) {
foreach ($row as $k => $v) {
echo $row [$k]. $row [$v]. <br/> ';
}
}
}else{
Echo ' Hu Jintao ';
} |
Add
Mysql_real_escape_string ()
So get the SQL statement if there is a similar way of writing:
"SELECT * from CDR where src =". $userId; will be changed to $userId =mysql_real_escape_string ($userId)
Example
| The code is as follows |
Copy Code |
|
<?php
$clean = Array ();
$mysql = Array ();
$clean [' last_name '] = "O ' Reilly";
$mysql [' last_name '] = mysql_real_escape_string ($clean [' last_name ']);
$sql = "INSERT
into User (last_name)
VALUES (' {$mysql [' last_name ']} ');
?> |
All printed statements such as echo,print should be filtered using htmlentities () before printing, so that you can prevent XSS, and note that Chinese should write
| code is as follows |
copy code |
|
Htmlentities ($name, ent_noquotes,gb2312). |