2017-03-31 163 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)“