我正在做一個PHP代碼段,將更新和刪除同一頁面的管理數據。我的更新查詢很好,我的刪除查詢是正確的,除了按Delete按鈕時它不能刪除數據。 代碼:刪除查詢是正確的,但不能刪除
<?php
require("connect.inc");
session_start();
if(isset($_POST['update_admin'])){
$id = $_POST['admin_id'];
$pass = htmlspecialchars(stripslashes(trim($_POST['admin_pass'])));
$name = htmlspecialchars(stripslashes(trim($_POST['admin_name'])));
$up_ad = "update admins set a_name = '$name', a_pass = '$pass' where a_id = '$id'";
mysql_query($up_ad);
}
if(isset($_POST['deladmin'])){
mysql_query("delete from admins where a_id = '$_POST[admin_id]'");
}
$admin_sql = "select * from admins order by a_name";
$adminres = mysql_query($admin_sql);
echo "
<table border = 1>
<tr>
<td>Admin ID</td>
<td>Admin Name</td>
<td>Password</td>
<td colspan = 3>Options</td>
</tr>
";
while($admin_rows = mysql_fetch_array($adminres)){
?>
<tr>
<form action = 'editdel_admin.php' method = post>
<td><?php echo $admin_rows['a_id'] ?></td>
<td><input type = text name = 'admin_name' placeholder = 'Type here' value = "<?php echo htmlspecialchars_decode($admin_rows['a_name']); ?>" required></td>
<td><input type = text name = 'admin_pass' placeholder = 'Maximum of 25 characters' maxlength = '25' value = "<?php echo htmlspecialchars_decode($admin_rows['a_pass']); ?>" required></td>
<?php
echo "
<td><input type = hidden name = 'admin_id' value = ".$admin_rows['a_id']."></td>
<td><input type = submit name = 'update_admin' value = 'Edit'></td>
<td><input type = submit name = 'deladmin' value = 'Delete'></td>
</form>
</tr>
";
}
echo "
</table>";
?>
你已經設置'$ id'到'$ _ POST [」 admin_id']',試試這個'mysql_query(「從管理員刪除,其中a_id ='$ id'」); ' – anurupr
OT:爲什麼在存儲用戶名和密碼時使用'htmlspecialchars'?這應該只在網頁上顯示文本時使用。 – Barmar
這個腳本有太多的錯誤,我不知道從哪裏開始。注射過載!我真的希望這不是一個專業的工作! –