How to turn this code in to a sql delete function?
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.
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.
精彩评论