我有一个名为aircondition的表(字段:ACName,Space,BTU,EnergyClass),我想编辑这些字段。我使用以下代码选择要编辑的空调:
test1.php
<?php
$username = "george";
$password = "george123";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("my_db", $dbhandle);
$table = "aircondition";
$sql = "SELECT * FROM aircondition";
$result = mysql_query($sql, $dbhandle);
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo $row['ACName']. "<a href='edit.php?edit=$row[ACName]'> Edit<br>
</a><br>";
}
}
?>
</body>
</html>
我有一个更新某个字段(例如BTU)。
edit.php
<?php
$username = "george";
$password = "george123";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("my_db", $dbhandle);
$id = intval($_GET['edit']);
if($id > 0) {
$res = mysql_query("SELECT * FROM aircondition WHERE ACNumber = '$id'");
$row= mysql_fetch_array($res);
$newbtu = mysql_real_escape_string($_POST['newbtu']);
$sql = "UPDATE aircondition SET BTU='$newbtu' WHERE ACNumber='$id'";
$res = mysql_query($sql) or die ("Error Updating".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=edit.php?edit=$id'>";
}
?>
<form action="edit.php?edit=<?= $id; ?>" method="POST">
<input type="text" name="newbtu" placeholer="test" /><br>
<input type="submit" value="Update" />
</form>
</body>
</html>
但是,这个似乎不起作用。虽然我没有得到任何错误,但在字段中没有任何更新。
答案 0 :(得分:0)
在这里,您要从ACName
获取详细信息,并在ACNumber
进行更新,因为@yajakass在评论中说明了这一情况。
修复它可以解决问题。