2017-03-31 162 views
-2

我想通過點擊一個鏈接刪除mysql數據庫中的記錄,但我無法完成它,我無法理解錯誤。 這裏是我的代碼無法通過PHP刪除mysql記錄

HTML

<a href="processCategory.php?action=delete?id=(<?php echo $id; ?>);">Delete</a> 

processCategory.php

<?php 
    require_once '../library/config.php'; 
    require_once '../library/functions.php'; 

    checkUser(); 

    $action = isset($_GET['action']) ? $_GET['action'] : ''; 
    switch ($action) { 

     case 'add' : 
      addCategory(); 
      break; 


     case 'delete' : 
      deleteCategory(); 
      break; 


     default : 
      // if action is not defined or unknown 
      // move to main category page 
      header('Location: index.php'); 
    } 


function deleteCategory() 
    { 
     if (isset($_GET['id']) && (int)$_GET['id'] > 0) { 
      $id = (int)$_GET['id']; 
     } else { 
      header('Location: index.php'); 
     } 

     // delete the products 
     $sql = "DELETE FROM tbl_vendors 
       WHERE id = $id"; 
     dbQuery($sql); 

     header('Location: ../vendor'); 
    } 


    ?> 
+0

哪裏是錯誤 –

+3

[小博](http://bobby-tables.com/ )說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

+0

不應該像這樣:'$ sql =「DELETE FROM tbl_vendors WHERE id =」。 $ id;'??你在說什麼錯誤? – lealceldeiro

回答

1

更換?與&和刪除"(" ")";在您的網址

<a href="processCategory.php?action=delete?id=(<?php echo $id; ?>);">Delete</a> 

您的腳本processCategory.php在變量ID收到此=(1)如你出

echo (int) "(1)"; 

輸出

0 

<a>標籤的正確代碼應爲

<a href="processCategory.php?action=delete&id=<?php echo $id; ?>">Delete</a> 
+4

您可能希望通過發佈正確的代碼而不是錯誤的代碼來更清楚地回答您的問題... – miken32

+0

感謝您的觀察,編輯 –

0

;「>刪除 刪除 」()「 可能在PHP獲得 」($ ID)「