son.of.the.morning Posted September 2, 2010 Share Posted September 2, 2010 I have finaly got my removing recods page working, but i do have one problem... i have a delete button which goes to a new php page which contains the DELETE FROM, this deletes the record automaticaly. There are two paths which i could choose from the problem is i am stuck on both of them. 1. a link that will activate the delete code. 2. once the record has been deleted then it will automaticaly go back the the first page. can any one help me here please? Quote Link to comment https://forums.phpfreaks.com/topic/212381-using-a-link-to-remove-a-record-from-a-database/ Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 The link for deleting the record will be <a href="delete.php?id=x">Delete</a> change X to your records id. Now in delete.php you'd do if(isset($_GET['id']) && is_numeric($_GET['id'])) { $id = (int) $_GET['id']; // dont forget to connect to mysql $query = "DELETE FROM table WHERE id=$id"; mysql_query($query); } You can redirect a visitor using header. But you can only use the function before your output any text/html. Quote Link to comment https://forums.phpfreaks.com/topic/212381-using-a-link-to-remove-a-record-from-a-database/#findComment-1106565 Share on other sites More sharing options...
son.of.the.morning Posted September 2, 2010 Author Share Posted September 2, 2010 i cant change the id to x, becuase i am passing a var through it, here is my code <?php $host="------"; // Host name $username="------"; // Mysql username $password="------"; // Mysql password $db_name="--------"; // Database name $tbl_name="forum_question"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $id=$_GET['id']; ?> ><a href="<? mysql_query("DELETE FROM forum_question WHERE id='$id'"); ?>">yes</a></p> Quote Link to comment https://forums.phpfreaks.com/topic/212381-using-a-link-to-remove-a-record-from-a-database/#findComment-1106570 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 I didn't say change your ids to x. I meant replace the x with your records id number. <a href="<? mysql_query("DELETE FROM forum_question WHERE id='$id'"); ?>"> You can not pass PHP code within a link. Look at my code example. Quote Link to comment https://forums.phpfreaks.com/topic/212381-using-a-link-to-remove-a-record-from-a-database/#findComment-1106572 Share on other sites More sharing options...
son.of.the.morning Posted September 2, 2010 Author Share Posted September 2, 2010 Ya i got that, i have it working now, i used a redirect in javascript so once the php has taken place it returns back to the record list page. you think that will be fine? Quote Link to comment https://forums.phpfreaks.com/topic/212381-using-a-link-to-remove-a-record-from-a-database/#findComment-1106575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.