循环更新语法


looping UPDATE syntax

我正在调整我在http://www.phpeasystep.com/mysql/10.html上找到的一个示例,以更好地满足我的需求。这是一个可以一次编辑所有结果的大表,而不是编辑单个记录。我已经修改了原来的代码,允许传递一个搜索词,以将表的内容限制为特定的条件。

如果你能帮我找错字,我将非常感激。在最初的查询$sql中,我必须稍微改变一下引号,以便它能与变量$location一起工作。更新一些行后,我点击提交,然后只看到表标题和提交按钮。没有内容,也没有更新到数据库。最后一个更新$ sq1与第一个非常相似,所以我不确定为什么它不起作用。我想弄清楚,如果没有什么问题与循环结构的例子。
<?php
$host="localhost"; // Host name 
$username="*****"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="Inventory"; // Database name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$location=$_POST['search']; 
echo $location; 
$sql = 'SELECT * FROM `Items` WHERE `Location` = "'.$location.'"';
$result=mysql_query($sql);
// Count table rows 
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Present Condition</strong></td>
<td align="center"><strong>Color</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<?php $id[]=$rows['ItemNumber']; ?> <?php echo $rows['ItemNumber']; ?>
</td>
<td align="center">
<input name="ItemName[]" type="varchar" id="ItemName" value="<?php echo $rows['ItemName']; ?>">
</td>
<td align="center">
<select name="ItemCondition[]" id="ItemCondition">
    <option value="">Select...</option>
    <option value="Excellent">Excellent !</option>
    <option value="Good">Good</option>
    <option value="OK">OK</option>
    <option value="Poor">Below Average</option>
    <option value="Change">Replace</option>
   </select>
</td>
<td align="center">
<input name="ItemColor[]" type="varchar" id="ItemColor" value="<?php echo $rows['ItemColor']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this 
if($Submit){
    for($i=0;$i<$count;$i++){
    $sql1="UPDATE `Items` SET ItemName='$ItemName[$i]', ItemCondition='$ItemCondition[$i]', ItemColor='$ItemColor[$i]' WHERE ItemNumber='$id[$i]'";
    $result1=mysql_query($sql1);
    echo $i;
    }
}
if($result1){
header("location:inventory.php");
}
mysql_close();
?>

这个表很简单。ItemNumber (key), ItemName, ItemCondition, ItemColor。当前,它查询项目列表,正确显示每个项目的值,但随后更新失败。我一头雾水。

提前万分感谢。

我注意到你的代码有几个问题,并为你重新编写了一些。希望评论/代码解释它,但请随时提出任何问题!

这没有经过测试,所以我提前为任何打字错误道歉。

<?php
$host = "127.0.0.1";
$username = "*****";
$password = "*****";
$db_name = "Inventory";
// When possible, you should use object-based SQL statements vs procedural
$sql = new mysqli($host, $username, $password, $db_name);
if($sql->connect_error) {
    die($sql->connect_error);
}

// This should really be checked with isset()
// It should also be sanitized to prevent SQL injections
$location = $_POST['search']; // This field should be sanitized to prevent SQL injections
$get_items = $sql->query("
    SELECT *
      FROM `Items`
     WHERE `Location` = '".$location."'
");
// count
$count = $get_items->num_rows;
// NOTE: Notice that the Submit logic comes before some of the other logic
// This is because of the header() statement. You can't change location if you've already outputted code
// One method around this is to buffer, but in this case, it's easier to just shift the code
if(isset($_POST['Submit'])) {
    for($i = 0; $i < $count; $i++) {
        $sql->query("
            UPDATE `Items`
               SET `ItemName`      = '".$_POST['ItemName'][$i]."',
                   `ItemCondition` = '".$_POST['ItemCondition'][$i]."',
                   `ItemColor`     = '".$_POST['ItemColor'][$i]."'
             WHERE `ItemNumber` = '".$_POST['id'][$i]."'
        ");
    }
    $sql->close(); // Close the connection on page-leave
    header("Location: inventory.php"); // Remember: You can't use this method to redirect if you've already displayed any content
    exit;
}
?>
<form name="form1" method="post">
    <table width="500" border="0" cellspacing="1" cellpadding="0">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Present Condition</th>
                <th>Color</th>
            </tr>
        </thead>
        <tbody>
        <?php
        while($row = $get_items->fetch_assoc()) {
        ?>
        <tr>
            <td align="center">
                <?php echo $row['ItemNumber']; ?>
                <input type="hidden" name="id[]" value="<?php echo $row['ItemNumber']; ?>">
            </td>
            <td align="center">
                <input name="ItemName[]" type="text" class="ItemName" value="<?php echo $row['ItemName']; ?>">
                <!-- This input type was "varchar" which isn't valid HTML -->
                <!-- Also, keep in mind that an ID should only be used once. Classes can be used multiple times, however. -->
            </td>
            <td align="center">
                <select name="ItemCondition[]" id="ItemCondition">
                    <option value="">Select...</option>
                    <option value="Excellent">Excellent !</option>
                    <option value="Good">Good</option>
                    <option value="OK">OK</option>
                    <option value="Poor">Below Average</option>
                    <option value="Change">Replace</option>
                </select>
            </td>
            <td align="center">
                <input name="ItemColor[]" type="text" class="ItemColor" value="<?php echo $row['ItemColor']; ?>">
            </td>
        </tr>
        <?php
        }
        ?>
        <tr>
            <td colspan="4" align="center">
                <input type="submit" name="Submit" value="Submit">
            </td>
        </tr>
        </tbody>
    </table>
</form>
<?php
$sql->close(); // Close the connection when done executing queries
?>