2012-12-08 32 views
-2

解決的問題前面部分,仍然需要理解爲什麼這是行不通的,雖然:)Backslashing撇號,因爲他們進入數據庫

回覆到單獨逃避串,我有這個。

mysql_query("INSERT INTO users (firstname,surname,email,password,birthday,birthmonth,birthyear,houseno,streetname,town,country,postcode,phonenumber,singer,songwriter,producer,composer,band,instrument,instrument2,extra,confirmcode) VALUES ('".$firstname."','".$surname."','".$email."','".$password."',".$birthday.",".$birthmonth.",".$birthyear.",'".$houseno."','".$streetname."','".$town."','".$country."','".$postcode."',".$phonenumber.",".$singer.",".$songwriter.",".$producer.",".$composer.", ".$band.",'".$instrument."','".$instrument2."','".$extra."','".$rand."')", 
    mysql_real_escape_string($surname), 
    mysql_real_escape_string($firstname), 
    mysql_real_escape_string($stown), 
    mysql_real_escape_string($houseno), 
    mysql_real_escape_string($streetname), 
    mysql_real_escape_string($extra), 
    mysql_real_escape_string($email)) or die ('pood'); 
    echo $streetname; 

我得到死亡錯誤 - 所以它不會回顯$ streetname; (這應該是'Clark's Way'),並且由於它沒有被輸入到數據庫中,所以似乎沒有反斜槓。

道歉,如果我還沒有完成我的研究到你的標準,但我一直在試圖理解爲什麼這不工作幾個小時。

謝謝:)

+12

脫出每個輸入值,而不是整個查詢。更好的是,通過[MySQLi或PDO](http://us3.php.net/mysqlinfo.api.choosing.php)使用參數化查詢,不用擔心逃跑。 – Wiseguy

+0

驗證是否所有的int字段都沒有被''和其他字段包圍 – Lunfel

+0

哦,經典的J-Dawg! –

回答

0

mysql_query()不採取多個參數那樣。您需要在範圍內跳轉每個變量查詢的字符串。就像這樣:

... VALUES ('".mysql_real_escape_string($firstname)."','".mysql_r... 

所以,你最終的東西是這樣的:

$query = "INSERT INTO users (firstname,surname,email,password,birthday, 
birthmonth,birthyear,houseno,streetname,town,country,postcode, 
phonenumber,singer,songwriter,producer,composer,band,instrument, 
instrument2,extra,confirmcode) VALUES (
'". mysql_real_escape_string($firstname) ."', 
'". mysql_real_escape_string($surname) ."', 
'". mysql_real_escape_string($email) ."', 
'". mysql_real_escape_string($password) ."', 
'". mysql_real_escape_string($birthday) ."', 
'". mysql_real_escape_string($birthmonth) ."', 
'". mysql_real_escape_string($birthyear) ."', 
'". mysql_real_escape_string($houseno) ."', 
'". mysql_real_escape_string($streetname) ."', 
'". mysql_real_escape_string($town) ."', 
'". mysql_real_escape_string($country) ."', 
'". mysql_real_escape_string($postcode) ."', 
'". mysql_real_escape_string($phonenumber) ."', 
'". mysql_real_escape_string($singer) ."', 
'". mysql_real_escape_string($songwriter) ."', 
'". mysql_real_escape_string($producer) ."', 
'". mysql_real_escape_string($composer) ."', 
'". mysql_real_escape_string($band) ."', 
'". mysql_real_escape_string($instrument) ."', 
'". mysql_real_escape_string($instrument2) ."', 
'". mysql_real_escape_string($extra) ."', 
'". mysql_real_escape_string($rand) ."')"; 

mysql_query($query) or die (mysql_error()); 

如果對不起有一個錯字在那裏。但我認爲你明白了。


下面是參數化的查詢可能是如何工作的(另)例如,改編自實例in the manual

$query = "INSERT INTO users (firstname,surname,email,password,birthday, 
birthmonth,birthyear,houseno,streetname,town,country,postcode, 
phonenumber,singer,songwriter,producer,composer,band,instrument, 
instrument2,extra,confirmcode) VALUES (
:firstname, :surname, :email, :password, :birthday, :birthmonth, 
:birthyear, :houseno, :streetname, :town, :country, :postcode, :phonenumber, 
:singer, :songwriter, :producer, :composer, :band, :instrument, :instrument2, 
:extra, :rand)"; 

// assume $dbh is your PDO database connection 
$sth = $dbh->prepare($query); 
$sth->bindParam(':firstname', $firstname); 
$sth->bindParam(':surname', $surname); 
$sth->bindParam(':email', $email); 
$sth->bindParam(':password', $password); 
$sth->bindParam(':birthday', $birthday); 
// and so on... 

$sth->execute(); 
+0

@ Joshua'J-Dawg'Oakley我會看看我是否還可以用參數化查詢(準備好的語句)製作一個示例,向您展示它的外觀。請記住,這都是未經測試的,但它應該給你一個總體思路。 :-) – Wiseguy

+0

感謝您花費這麼做的時間,我試着自己打字並且只轉義相關的(不是整數),但是我得到某種語法錯誤(這些錯誤在我的服務器出於某種原因),然後我嘗試複製你寫的內容,並得到同樣的問題。我會嘗試撤消所有的操作,並且一次添加1個轉義字符串 –

+0

@ Joshua'J-Dawg'Oakley我應該暫時待一會兒,所以如果你想繼續討論這個問題,你會怎麼樣[加入我在聊天](http://chat.stackoverflow.com/rooms/20783/),所以我們不會混淆在這個頁面上的評論。 – Wiseguy

0

錯誤歸因於反斜槓引號。刪除反斜槓,它看起來應該工作。

前:VALUES (\'Joshua\',\'Oakley\',
後:VALUES ('Joshua','Oakley',

+0

是的,我想到了這個,有沒有辦法改變$查詢來停止在開始和結束撇號反斜槓? :) –

相關問題