2011-11-23 40 views
0

,當我張貼的東西進入我的HTML形式,例如在第一個名稱字段,我輸入:SQL語法錯誤/字符recogition

'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."')"; 

回答

1

SQL插入語句容易受到SQL注入的影響。如果其中一個POST值包含雙引號"或換行符,則語句會被破壞並出現語法錯誤。確保你逃脫用戶提供的所有信息mysql_real_escape_string()

0

使用mysql_real_escape_string功能上mysql_real_escape_string($_POST[firstname'])。事實上,在將它傳遞給SQL之前,在所有的後置變量上執行它。

+0

我是否需要定製功能? – mayman212

+0

是的,在將它傳遞給服務器之前,請始終過濾輸入。否則,它很容易SQL注入...! –

+0

這是有效的嗎? ''.mysql_real_escape_string($ _ POST [firstname])。「','」。mysql_real_escape_string($ _ POST [lastname])。'' – mayman212