【问题标题】:php won't return entire value from databasephp不会从数据库返回整个值
【发布时间】:2014-11-01 00:17:30
【问题描述】:

我目前正在处理在线订单,遇到了一个非常奇怪的问题。 我有一个下拉菜单,哪些选项是数据库表中某一列的值。这是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>

当我要处理placedOrder.php 以从html 中返回值并存储到数据库中时,我一直让页面返回空白并且没有任何显示。我发现原因可能是“描述”部分。您可能会在以下代码中看到:

<?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];
    } 

?>

$_POST['description'];部分应该从数据库中返回我的产品名称,它是“48X72 CORDLESS BLACKOUT CELLULAR SHADE 9/16”WHITE”但是在我回显它后只返回“48X72”,其余的值都消失了。我错过了代码中的任何内容吗?

【问题讨论】:

  • 警告您的代码极易受到 sql 注入攻击!!!
  • 好的...因为我是新手并且是自学的,所以我不确定sql注入攻击到底是什么,但谢谢!我会试着找出答案
  • 开始使用PDOmysqli_* 函数并在来回发送数据之前绑定您的值。通过这种方式,您将能够防止大多数注入漏洞。 mysql_* 函数已正式弃用并且不推荐使用

标签: php html mysql


【解决方案1】:

在 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,因为“这一直是这样做的”。

【讨论】:

  • 有效!非常感谢。我没想到那部分会出现问题,因为我之前做过相同类型的编码,而且效果很好,所以..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多