php将某些字段更新到数据库

时间:2014-11-04 12:48:10

标签: php html mysql

我尝试使用库存系统,用户可以查看库存并使用仅由用户输入的值更新数量,并且数据库中的其余值保持不变。但它不起作用请帮助我找到我做错的地方。它将回显成功消息,但数据库未更新。

<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!';
}
?>

3 个答案:

答案 0 :(得分:2)

您在这里使用mysql_query

$sql = "UPDATE products SET quantity ='".$value."' WHERE sku_id = '".$sku[$key]."'";
mysql_query($sql);

而不是mysqli_query

$sql = "UPDATE products SET quantity ='".$value."' WHERE sku_id = '".$sku[$key]."'";
mysqli_query($sql);

此外,您还在这里使用mysql_error

die('Could not update data: '. mysql_error());

P.S。不要忘记转义您在数据库查询中使用的任何用户输入!虽然理想情况下你应该使用类似PDO或MySQLi准备语句的东西

答案 1 :(得分:0)

如果你做var_dump($ _ POST);你会看到你的输入没有值。

您需要在表单上指定值。

我宁愿这样做:

echo '<input name="sku['.$row['sku_id'].']" value="'.$row['quantity'].'" />';

然后你可以循环通过$ _POST ['sku']并使用Key作为sku_id,将Value作为新值(数量),进行更新

答案 2 :(得分:0)

这应该是一个完整的答案(使用mysqli更新):

<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">Quantity</th>
<th scope="row">Unit Price</th>
</tr>
<?php
include('connect.php');
$result = mysqli_query("SELECT * FROM products") 
        or die(mysqli_error());

while($row = mysqli_fetch_array( $result )) {
    echo "<tr>";
    echo '<td>'.htmlspecialchars($row['sku_id']).'</td>';
    echo '<td>'.htmlspecialchars($row['description']).'</td>';
    echo '<td><input name="qty['.htmlspecialchars($row['sku_id']).']" value="'.htmlspecialchars($row['quantity']).'"/></td>';
    echo '<td><input name="price['.htmlspecialchars($row['sku_id']).']" value="'.htmlspecialchars($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'];
    $price = $_POST['price'];
    $stmt =  $mysqli->stmt_init(); // <- mysqli class way of doing this
    $stmt->prepare("UPDATE products SET quantity = ?, unit_price = ? WHERE sku_id = ?");
    foreach($qty as $key => $value) 
    {
        $data = array($qty[$key], $price[$key], $key);
        $stmt->execute($sql, $data);
    }
    echo 'Update data successfully!';
}
?>

出于测试目的,可以将帖子的处理更改为:

if(isset($_POST['update']))
{
    $qty = $_POST['qty'];
    $price = $_POST['price'];
    //$stmt =  $mysqli->stmt_init(); // <- mysqli class way of doing this
    //$stmt->prepare("UPDATE products SET quantity = ?, unit_price = ? WHERE sku_id = ?");
    foreach($qty as $key => $value) 
    {
        echo "UPDATE products SET quantity = ".$qty[$key].", unit_price = ".$price[$key]." WHERE sku_id = " . $key . "<br/>\n";
        //$data = array($qty[$key], $price[$key], $key);
        //$stmt->execute($sql, $data);
    }
    echo 'Update data successfully!';
}