-2

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

我将非常感谢一些帮助找到我的错字。在初始查询 $sql 中,我不得不稍微更改引号,以便它可以与我的变量 $location 一起使用。更新一些行后,我点击提交,然后只看到表头和提交按钮。数据库没有内容,也没有更新。最后一个更新 $sql1 与第一个更新非常相似,所以我不确定它为什么不起作用。我试图弄清楚示例中的循环结构是否有问题。

<?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(键)、ItemName、ItemCondition、ItemColor。目前它查询项目列表,正确显示每个项目的值,但随后无法更新。我难住了。

一百万提前谢谢。

4

1 回答 1

0

我注意到您的代码存在一些问题,并为您重新编写了其中的一些。希望评论/代码解释它,但随时提出任何问题!

这未经测试,所以我提前为任何错别字道歉。

<?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
?>
于 2013-04-22T22:26:54.720 回答