Jump to content

[SOLVED] Delete table row hyperlink


doddsey_65

Recommended Posts

I currently have a user managment system im working on. What i want is to have a hyperlink next to the rows that are pulled(see code below). with this button i can delete the respective row from the db therefore deleting the user. Here is the code i am using to display their details:

 

$sql = "SELECT name, email, date FROM users";
$res = mysql_query($sql, $db);
while($row = mysql_fetch_array($res, MYSQL_ASSOC))

{

echo $row['name'] . ": " . $row['email'] . " -- " . $row['date'];
echo "<br />";

}

I know how to delete records, what i dont know how to do is to delete the record I want ie. the one next to the delete link if you know what i mean.

Link to comment
Share on other sites

I presume you have a unique ID column for the table users right?

 

yes, but how would i get the hyperlink to know its the user next to it that i want it to delete

 

Eg. User: Tom [delete]

    User: Dick [delete]

    User: Harry[delete]

 

How would i delete tom with the code i am using to display them

Link to comment
Share on other sites

here is an example:

 

if($_GET['method']=='delete' && $_GET['id']){
$query = "DELETE FROM table WHERE id='".$_GET['id']."'"; //delete the entry from the database
mysql_query($query); //make the queyr
}

$sql = "SELECT name, email, date FROM users";
$res = mysql_query($sql, $db);
while($row = mysql_fetch_array($res, MYSQL_ASSOC))

{

echo $row['name'] . ": " . $row['email'] . " -- " . $row['date'];
echo '<a href="thecurrentpage.php?method=delete&id='.$row['id'].'">Delete</a>';
echo "<br />";

}

 

All you are doing is creating a link with a $_GET post(in a sense). Then if that is set you are deleting it and if not it doesnt delete it. Obviously in this example you would have to have unique id in the database and you would change the href to the page that you are currently on.

Link to comment
Share on other sites

@emopoops that makes no sense that he would delete a user by the url. you should never delete a user by a name or age or some other value that can be in another users field. There should be a unique value in the database that identifies the user. Hence the use of an incrementing id. Then you can delete it by using its UNIQUE id. Almost every table I make in the database has a unique id.

Link to comment
Share on other sites

@emopoops that makes no sense that he would delete a user by the url. you should never delete a user by a name or age or some other value that can be in another users field. There should be a unique value in the database that identifies the user. Hence the use of an incrementing id. Then you can delete it by using its UNIQUE id. Almost every table I make in the database has a unique id.

 

same here, i have an ai on an id table. cheers for the help people. I did think halfway through however why dont i just gointo the database and delete them, but im going to use this way anyway cos my db loads too slowly. Cheers

Link to comment
Share on other sites

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.