2012-12-13 37 views
1

我有一個mySQL查詢,當我運行一個包含'@'的字符串時卡住了。我試過htmlentities()和htmlspecialchars()無濟於事。以下是我正在運行的是什麼:從'@'開始的SQL語法錯誤

$name=$_POST['name']; 
$first=$_POST['first']; 
$last=$_POST['last']; 
$bio=htmlentities($_POST['bio']); 
$email=htmlentities($_POST['email']); 
$pass=$_POST['pass']; 
$date=date("m/d/y"); 
$bd=date('m-d-y',strtotime($_POST['month'].$_POST['date'].$_POST['year'])); 
$qer="insert into everything (user,first,last,bio,email,pass,date,bd) values ($name,$first,$last,$bio,$email,$pass,$date,$bd)"; 
if(!(mysql_query($qer,$con))){ 
    echo "no qer"; 
    echo mysql_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 '@re5yhgr5tyhrtyhr5tyr5tyhrt,[email protected],pass,12/13/12,01-01-70)' at line 1

我是第一次嘗試,它只是在我的電子郵件參數,但現在我知道它有麻煩,無論它在哪裏。 >:|

我假設「1號線」是SQL的第一行是因爲我的實際線路1「」 ......提前

很抱歉,如果這是顯而易見的,謝謝!

+0

我喜歡那個表名, 「一切」 – kennypu

+0

**請閱讀:** http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php –

回答

3

當插入數據類型爲string的值時,應該用括起來的。 (同樣日期,時間,日期時間等,只要它不是數字

$qer="insert into everything (user, first, last, bio, email, pass, date, bd) 
     values ('$name', '$first', '$last', ...)"; 

,但上面的查詢與SQL Injection脆弱的,請閱讀下面的文章,以瞭解如何從SQL Injection

防止
+0

很酷,謝謝! – user1801821

+2

@KuyaJohn關於SQL注入的重點。 PHP初學者傾向於構建可能被惡意編碼器一秒鐘取下的網站。 – Anton

+0

因爲我寧願不把我的東西切換到sqli或pdo,mysql_real_escape_string()會爲此工作正常嗎?編輯:nvm ... – user1801821

0

你應該換你的字符串中插入SQL參數數量,像"[email protected]"

+0

這忽略了一點。 OP應該使用準備好的語句。 –

2

嘗試:在類型爲varchar,char或date的字段中添加撇號(')。

...values ('$name','$first','$last','$bio','$email','$pass','$date','$bd')... 
+2

儘管這確實回答了這個問題,但它也會使代碼對SQL注入攻擊開放。至少,在插入數據庫查詢字符串之前,對所有內容使用mysql_real_escape_string,或者甚至更好地使用安全,高效並且不需要引號的PDO或mysqli。 – Anton

+1

@Anton'mysql_real_escape_string'不能完全防止你從sql注入,請看這裏:[SQL注入獲取mysql_real_escape_string()](http://stackoverflow.com/questions/5741187/sql-injection-that-gets- around-mysql-real-escape-string/5741264#5741264) –

+0

好的和有用的評論。謝謝。 – alditis