我正在调整我在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。目前,它查询项目列表,正确显示每个项目的值,但无法更新。我很难过。
提前一百万谢谢。答案 0 :(得分: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
?>