php不会从数据库中返回整个值

时间:2014-11-01 00:17:31

标签: php html mysql

我目前正在处理在线订单并遇到一个非常奇怪的问题。 我有一个下拉菜单,哪些选项是数据库表中一列的值。这是html代码:

<form name="form" method="post" action="placedOrder.php"><table width="70%" border="5" align="center"><tr>
<th scope="row">Item Name</th>
<th scope="row">Item SKU</th>
<th scope="row">Quantity</th>
<th scope="row">Special Note</th>
<th scope="row">Unit Price</th>
<th scope="row">Total Price</th></tr><tr>
<th scope="row">
<?php
include('connect.php');

$result = mysql_query("SELECT description FROM products") 
            or die(mysql_error());
print "<select name='description' value='description'>";
print "<option value='' disabled selected>Please Select A Product</option>";
while ($info = mysql_fetch_array($result))
{
        $p = $info["description"];
        print "<option value=$p>".$p."</option>";
}
print "</select>";
?>
</th>
<th scope="row"><input name="sku_1" id="sku_1" readonly /></th>    
<th scope="row"><input name="qty_1" /></th>
<th scope="row"><input name="note_1" /></th>  
<th scope="row"><input name="uPrice_1" id="uPrice_1" readonly /></th>
<th scope="row"><input name="tPrice_1" readonly /></th></tr></table><input type="submit"/></form>

当我打算在placementOrder.php上处理来自html和存储到数据库的返回值时,我一直将页面返回空白并且没有显示任何内容。我发现原因可能是&#39;描述&#39;部分。您可以在以下代码中看到:

<?php   
include('connect.php');
$p = $_POST['description'];
echo $p;
$result = mysql_query("SELECT sku_id, unit_price FROM products WHERE description='{$_POST['description']}'")
            or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
            echo $row[0];
            echo $row[1];
    } 

&GT?;

$ _POST [&#39;说明&#39;]; part应该从数据库中返回我的产品名称,这是&#34; 48X72 CORDLESS BLACKOUT CELLULAR SHADE 9/16&#34; WHITE&#34;但是在我回应它之后只返回&#34; 48X72&#34;,剩下的值就消失了。我错过了代码中的任何内容吗?

1 个答案:

答案 0 :(得分:3)

以HTML格式引用您的值,并转义数据。

print "<option value=$p>".$p."</option>";

要:

print "<option value=\"".htmlspecialchars($p)."\">".htmlspecialchars($p)."</option>";

我更喜欢它写的:

$p = htmlspecialchars($p);
printf('<option value="%s">%s</option>', $p, $p);

你只得到了第一部分,因为在字符串中的第一个空格之前就已存在,浏览器将其解释为值,其余部分为语法错误。

就此而言,应引用HTML中的所有属性值:

<tag stringproperty="value" integerproperty="42"></tag>

如果你想真的严格,唯一允许的引号是双引号。

然而,大多数浏览器在一个或多或少永久的“怪癖”模式下运行并接受/呈现各种违反HTML的标准,因为“这就是它总是如此完成”。