2010-10-12 95 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