2016-03-08 25 views
-1

我有一個表單將一些數據發送到成功頁面,並將它們插入到數據庫中。當從表單中獲取數據時,在MySQL中插入一個包含引號的值

因此,我無法控制從 表單中的數據中有什麼。如果用戶使用單引號,則數據不會到達 數據庫。我如何確保用戶不要在表單中使用單引號?

這裏是我的代碼:

$details = $this->getDetails(); 
$conferance_name = $details['product_name']; 
$hotel = $details['hotelval']; 
$datefor = $details['dateval']; 
$ogtitel = $details['navn_title']; 
$virksomhed = $details['virksomhed']; 
$adresse = $details['adresse']; 
$postnr = $details['postnr']; 
$by = $details['by']; 
$telefon = $details['telefon']; 
$email = $details['email']; 
$kommentarer = $details['kommentarer']; 
$ean = $details['ean']; 
$entries = $details['entries']; 
$newdate = date('Y-m-d') 


$conn = Mage::getSingleton('core/resource')->getConnection('core_write'); 
$sql = "INSERT INTO mage_emailorder (emailorder_id, order_date, product_name, location, date_for, name_title, email, company, adress, zipcode, city, phone, ean, comment, order_done, number_of_entries) 
VALUES (NULL, '$newdate', '$conferance_name', '$hotel', '$datefor', '$ogtitel', '$email', '$virksomhed', '$adresse', '$postnr', '$by', '$telefon', '$ean', '$kommentarer', '0', '$entries')"; 

所有的變量是從一種形式,我要保護的形式,從特殊符號。

+2

聽起來你插入數據直入SQL,這意味着你容易受到SQL注入。使用[預準備語句](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – frosty

+1

@Desas * no *。決不。 'addslashes()'不*使字符串安全地插入到數據庫中。 –

+0

這個問題需要更多的上下文和代碼。你能解釋一下你使用的是什麼技術,並且可能會顯示代碼中的一些相關位(比如插入查詢)? –

回答

相關問題