我正在尝试在库存系统上工作,用户可以在其中查看其库存和更新数量以及仅由用户输入的值并且数据库中的其余部分保持不变。但它不工作,请帮我找到我做错了什么。它会回显成功消息,但数据库不会更新。php将某些字段更新到数据库
<form name="form" method="post">
<table width="70%" border="5" align="center"><tr>
<th scope="row">SKU</th>
<th scope="row">Item Description</th>
<th scope="row">Current Qunatity</th>
<th scope="row">Update Quantity</th>
<th scope="row">Unit Price</th>
</tr>
<tr>
<th scope="row">
<?php
include('connect.php');
$result = mysqli_query("SELECT * FROM products")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td><a name="sku[]">'.$row['sku_id'].'</a></td>';
echo '<td>'.$row['description'].'</td>';
echo '<td>'.$row['quantity'].'</td>';
echo '<td><input name="qty[]" /></td>';
echo '<td>'.$row['unit_price'].'</td>';
echo "</tr>";
}
?>
</table>
<input style="float:right" name="update" type="submit" id="update" value="Update"/>
</form>
<?php
if(isset($_POST['update']))
{
$qty = $_POST['qty'];
$sku = $_POST['sku'];
foreach($qty as $key => $value)
{
if(empty($value))
{
continue;
}
else
{
$sql = "UPDATE products SET quantity ='".$value."' WHERE sku_id = '".$sku[$key]."'";
mysql_query($sql);
}
}
$retval = mysqli_query($sql);
if(! $retval)
{
die('Could not update data: '. mysql_error());
}
echo 'Update data successfully!';
}
?>
这样的事情是很容易避免与一个简单的错误输出 – baao 2014-11-04 12:52:48
@Joseph没有关系到你的问题,但为什么你不使用PDO - 没有SQL注入,推荐。 mysql_query已弃用 – 2014-11-04 13:29:24
感谢您的建议!我会发现PDO的工作原理! – Joseph 2014-11-04 13:51:42