Jos*_*vis 2 php security sql-injection
我担心sql注入,所以我该如何预防呢?我正在使用这个脚本,但有几个人告诉我它非常不安全,如果有人可以通过告诉我它会如何变得很好:).
源代码:
if(isset($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$result=mysql_query("SELECT * FROM updates WHERE item_id<'$lastmsg' ORDER BY item_id DESC LIMIT 16");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['item_id'];
$message=$row['item_content'];
Run Code Online (Sandbox Code Playgroud)
永远不会将用户($_POST或$_GET)的信息直接放入查询中.如果它们是数字,请始终先将它们转换为整数(int)$var或intval($var); 如果他们是字符串,总是逃避它们mysql_real_escape_string().
阅读http://us2.php.net/mysql_real_escape_string并使用它.