mysql - SQL语法错误/字符识别

标签 mysql function syntax syntax-error

当我将某些内容发布到 html 表单中时,例如在名字字段中,我输入:

'John', i am getting the following error:

查询错误: 。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Smith',Address_Line_1='rtuy657tr',Address_Line_2='',City='leicester',Postcode='L' at line 1

我知道它与mysql_real_escape_string ()函数有关,但是我将如何使用它来插入数据库。我已经启动了该功能:

function db_insert_preparation(){



}


$con = mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("project1", $con);

这是需要使用它的地方:

$sql2 = "INSERT INTO `".$table_name."` (`Group`, `Date_Of_Birth`, `Gender`, `Title`, `First_Name`, `Last_Name`, `Address_Line_1`, `Address_Line_2`, `City`, `Postcode`, `Contact_No`, `Email`, `Additional_Comment`, `Upload_File`) VALUES ('".db_insert_preparation($group)."','".$_POST[dateofbirth]."','".$_POST[gender]."','".$_POST[title]."','".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[address1]."','".$_POST[address2]."','".$_POST[city]."','".$_POST[postcode]."','".$_POST[contactno]."','".$_POST[email]."','".$_POST[note]."','".$filename."' )";

最佳答案

SQL插入语句容易受到SQL注入(inject)攻击。如果 POST 值之一包含双引号 " 或换行符,则语句会损坏并出现语法错误。请确保使用 mysql_real_escape_string() 转义用户提供的所有内容。

关于mysql - SQL语法错误/字符识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8240332/

相关文章:

php - 如何在更复杂的 OOP 概述中正确构造类和方法

php - MySQL 多插入? INSERT 失败后的 MySQL 数据库完整性

android - 创建以日期作为列标题的 mysql 表

javascript - 如何重构多个 if...else 以提高效率?

function - 传递多个参数

c - 结构数组的参数类型不兼容

javascript - 评论在 JS 中是如何分类的?

php - mysql查询,选择一定的结果数组

c - 短语 (int const * const b) 在 C 中是什么意思?

python - 将 CSV 数据插入 MySQL 数据库 - "You have an error in your SQL syntax; near ' ' 在第 1 行