0

I was wondering how I could go about turning this add entry link in to a delete entry link? Any help would be appreciated.

if (CAN_EDIT){
    echo '<i>'.$announcements[$x]['page_edit']='<a href="index.php?id='.$announcements[$x]['page_id'].'&act=edit">edit entry</a></i><br />';
if ($announcements[$x]['page_edit']>0){}
                        }

I understand about using

mysql_query DELETE FROM (table name) WHERE (item name)

I am just not sure how to implement it and make it a link.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nicole
  • 217
  • 2
  • 7

1 Answers1

2

It's really up to how your application handles the requests. The edit link code you posted shows that two parameters are being passed to the PHP: id and act. Both will be available within $_GET in PHP when the link is clicked.

From that, we can infer that your code is checking $_GET['act'] to decide what to do. If it is equal to 'edit', you'll probably fetch some data from the DB (for records where id equals $_GET['id']), then render an edit form.

Using that same logic, you should create a link with id=N&act=delete (where N is an actual ID). Then in PHP you check if the value of $_GET['act'] is 'delete', then run the SQL query to delete the row with the ID passed.

Also, make sure to do a little research on SQL injection -- you should not use $_GET or $_POST values directly in the query, as that would make your app vulnerable to injection. Take a look at the mysql_real_escape_string function on the PHP manual.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • 2
    Do not underestimate the importance of the last paragraph in this answer, especially when working with publicly readable `$_GET` parameters! Keeping that in mind, read JKirchartz' comment on your question and immediately forget about it. Very bad advice that will get you in trouble sooner or later. Always clean user input before using it in queries. – Rem.co Aug 31 '11 at 21:14
  • +1 @Remco. The solution works great until you run across the id ";DROP ALL TABLES", at which point you've got problems. – PaulStock Aug 31 '11 at 21:38
  • There will only be a handful of people who will have access to be able to delete entries. And they wont be able to do it unless they are logged in to the system. – Nicole Sep 01 '11 at 13:29