Jump to content

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/207861-deleting-mysql-data-with-link/
Share on other sites

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

 

<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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.