0
我有一個表命名AIRCONDITION(字段:ACName,空間,BTU,EnergyClass),我想編輯這些field.I使用下面的代碼來選擇要編輯的AIRCONDITION:編輯表值碼不起作用
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>
然而,這一次似乎並沒有工作。雖然我沒有得到任何的錯誤,但是在這些領域沒有更新。
您正在從ACName中獲取並更新ACNumber。你確定這兩個字段具有相同的值嗎? – yajakass 2014-09-22 17:07:51
MySQL函數的折舊年限爲PHP 5.5中,請使用MySQLi函數。 – Edward 2014-09-22 17:15:54
謝謝你,我的壞。我下次會更小心,以避免這樣的錯誤。 – george123 2014-09-22 17:16:00