我在HTML创建的窗体(查询形式)的帖子下面的代码:表单验证PHP /避免注射
<?php
if(isset($_POST['submit']))
{
$name = mysql_real_escape_string((string)$_POST['name']);
$surname = mysql_real_escape_string((string)$_POST['surname']);
$email = mysql_real_escape_string((string)$_POST['email']);
$phone = mysql_real_escape_string((string)$_POST['phone']);
$country = mysql_real_escape_string((string)$_POST['country']);
$message = mysql_real_escape_string((string)$_POST['message']);
$sql = "INSERT INTO contact
(name, surname, email, phone, country, message)
VALUES('$name', '$surname', '$email', '$phone', '$country', '$message')";
mysql_select_db($db);
$retval = mysql_query($sql, $conn)or die(mysql_error());
echo 'Thank you '.$name.' '.$surname.'. Your enquiry has been forwarded to our team. <br><br>Please check you email inbox for further information.<br><br>Return to homepage:<br><br><button class="search" onclick="/">Return to homepage</button>';
mysql_close($conn);
}
?>
我很纳闷,我怎么能显示错误和无效时,停止形式发布或输入零数据?
在学习如何在网上创建表单时,我也听说过SQL注入。我受保护了吗?
非常感谢。
...只要你逃脱所有的**字符串值**你在一个SQL查询,而不是变量嵌入。 –
@Col。基本上,所有提交的值都是字符串。数值('“123”')和整数('123')之间是有区别的。因此,我发现最好将int列的值转换为整数,此外还要转义字符串输入。 –
我的意思是对于SQL来说,唯一的字符串值是一个用引号括起来的值 –