2010-10-12 96 views
1

我無法讓我的addslashes函數和html選項值在一起玩。我最初的問題是選項中的單引號,但通過解決,我似乎已經創建了另一個問題,即$ titleunit_name只能通過第一個單詞出現。php表單中的單引號和addslashes(選項值在空間上轉義?)

這是我想出來的:

baroffice =奧法倫&公路ķ&ň

titleunit_name =奧法倫&公路ķ&ň

cleantitleunit_name = O \」 Fallon & Highway K & N

這就是我得到的:

baroffice =奧法倫

titleunit_name =奧法倫&公路ķ&ň

cleantitleunit_name = O \'法倫&公路ķ&ň

我不知道,如果它的事項但值通常來自並被髮回到MS SQL。

<form method="post" action="formtest.php?" id="searchform" target="" autocomplete="off"> 


<div id="office"> 

<font style="font-size:12px; font-weight:bold;" color="#002EB8" face="Verdana"> 
Closing Office:</font> 

<select name="baroffice" style="width:90px"> 
<?php 
$titleunit_name= "O'Fallon & Highway K&N"; 
$cleantitleunit_name=addslashes("$titleunit_name"); 

echo "<option value=$cleantitleunit_name name= '$titleunit_name'>"; 
echo "$titleunit_name</option>"; 

?> 
</select></div><br> 
<br><Br> 
<input type="submit" name="submit" value="submit" style="position:relative;z-index:3"> 
<br><Br> 
</form> 

<?php 
$baroffice = str_replace("\'","'",($_POST['baroffice'])); 

if (isset($_POST['submit'])) 
{ 

echo "baroffice=$baroffice<br>"; 
echo "titleunit_name=$titleunit_name<br>"; 
echo "cleantitleunit_name=$cleantitleunit_name<br>"; 


} 
else 
{echo ""; 
}; 

?> 

感謝您的任何幫助提前。

回答

4

首先,你不需要在變量周圍加雙引號。只是$titleunit_name是正確的,而不是"$titleunit_name"

其次,從不使用addslashes。如果您正在轉向內容以進入MySQL,請使用更強大的mysql_real_escape_string函數。 addslashes錯過了案例,並且讓你的腳本每一點都像攻擊一樣開放,就好像你根本沒有使用過它一樣。

最後,斜槓不屬於HTML輸出。您正在尋找htmlspecialchars函數,該函數準備要寫入HTML文檔的字符串。

echo '<option value="' . htmlspecialchars($titleunit_name) . '" name="' . htmlspecialchars($titleunit_name) . '">' . htmlspecialchars($titleunit_name) . '</option>'; 

注意$titleunit_name所有使用(或任何其他變量)必須這樣寫出來的頁面之前進行轉義。

現在,我是從上下文中猜出,你有「魔術引號」,所以PHP會自動對傳入的POST數據執行addslashes。如果是這樣,請關閉魔術引號,然後在需要將字符串插入數據庫時​​執行相應的轉義。如果這是不可能的,則在腳本執行開始時使用stripslashes從所有POST數據中剝離斜線,以便獲取提交的數據。

+0

感謝您的幫助。它實際上不是mysql,而是ms sql的odbc。你還糾正了很多我的格式,並且做到了! – bnw 2010-10-12 21:19:55