dominod Posted July 15, 2010 Share Posted July 15, 2010 Hi I have a list of mysql data entries on a webpage like: 1 name1 email1 2 name2 email2 3 name3 email3 ect. How can I delete an entire row (id, name and email) from that database using a link so it will look like: 1 name1 email 1 - delete this (clicking on this link will delete this row) I Googled around and found this: http://www.totallyphp.co.uk/code/delete_data_from_a_mysql_database.htm <?php /* * Change the first line to whatever * you use to connect to the database. * * Change tablename to the name of your * database table. * * This example would delete a row from * a table based on the id of the row. * You can change this to whatever you * want. */ // Your database connection code db_connect(); $query = "DELETE FROM tablename WHERE id = ('$id')"; $result = mysql_query($query); echo "The data has been deleted."; ?> What I dont understand is how to trigger this when clicking on a link .. :?: Anyone got some suggestions? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/ Share on other sites More sharing options...
litebearer Posted July 15, 2010 Share Posted July 15, 2010 Psuedo code (single and double quotes need to be added)... 1. start looping thru your list 2. echo <a href=deletion_page.php?id=the id to delete>echo the name1 email1</a> 3. end loop then deletion_page would GET the id and do a query deleting that record. go back to first page and start again Quote Link to comment https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/#findComment-1086621 Share on other sites More sharing options...
runthis Posted July 15, 2010 Share Posted July 15, 2010 <a href=?del=1>1 name1 email1</a> <a href=?del=2>2 name2 email2</a> <a href=?del=3>3 name3 email3</a> if(isset($_GET['del'])){ mysql_query("DELETE FROM tablename WHERE id='{$_GET['del']}'"); } if its for something like user accounts, make a field named 'active' and if its being deleted, don't actually delete it, set active to 0 and on your pages you can have a simple little if($active == '0'){ print "This account is inactive"; die; } in your header Quote Link to comment https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/#findComment-1086622 Share on other sites More sharing options...
AbraCadaver Posted July 15, 2010 Share Posted July 15, 2010 Don't use get for modifications/deletions. You should always use post. You can use a get request like: <a href=deletion_page.php?id=the id to delete>echo the name1 email1</a> But then that page should show a confirmation form that posts to itself or other page that does the deletion. Quote Link to comment https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/#findComment-1086630 Share on other sites More sharing options...
runthis Posted July 15, 2010 Share Posted July 15, 2010 <form method="post"> <select name=del> <option value=1>name1 email1</option> <option value=2>name2 email2</option> <option value=3>name3 email3</option> </select> </form> if(isset($_POST['del'])){ mysql_query("DELETE FROM tablename WHERE id='{$_POST['del']}'"); } Quote Link to comment https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/#findComment-1086633 Share on other sites More sharing options...
dominod Posted July 15, 2010 Author Share Posted July 15, 2010 Thanks alot guys Problem solved ! Quote Link to comment https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/#findComment-1086672 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.