2014-03-12 85 views
0

我正在做一個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>"; 
?> 
+0

你已經設置'$ id'到'$ _ POST [」 admin_id']',試試這個'mysql_query(「從管理員刪除,其中a_id ='$ id'」); ' – anurupr

+0

OT:爲什麼在存儲用戶名和密碼時使用'htmlspecialchars'?這應該只在網頁上顯示文本時使用。 – Barmar

+0

這個腳本有太多的錯誤,我不知道從哪裏開始。注射過載!我真的希望這不是一個專業的工作! –

回答

2

檢查用戶是否有權刪除記錄在數據庫的權限。

更改刪除查詢

"delete from admins where a_id = '" . $_POST['admin_id'] . "'"; 
+0

這應該是一個評論... – War10ck

0

試試這個

mysql_query("delete from admins where a_id = '".$_POST['admin_id']."'"); 

代替

mysql_query("delete from admins where a_id = '$_POST[admin_id]'"); 
+2

''「。intval($ _ POST [admin_id])。」''防止sql注入 – Jack

0

你錯在這一行

mysql_query("delete from admins where a_id = '$_POST[admin_id]'"); 

$_POST[admin_id]這裏未正確指定鍵值。它一定是這樣的 $_POST['admin_id']。並在嵌入字符串中用戶括號避免貪婪方法字符串替換。改變你的代碼像這樣將能解決問題

mysql_query("delete from admins where a_id = '{$_POST['admin_id']}'"); 

要注意的是mysql_ *方法現在已經過時和不安全的,而不是使用mysqli_ *或PDO方法