Jump to content

Recommended Posts

I am trying to build a simple content management system and am not real sure where to go.  I have the code (below) that displays my table contents.  Works great!  What I want to do is make that "delete" word clickable and execute a delete function of that row's data.

 

<?php

 

$result = mysql_query("SELECT * FROM admin");

 

echo "<table border='1'>

<tr>

<th>First Name</th>

<th>Last Name</th>

<th>Email Address</th>

<th>DELETE USER</th>

</tr>";

 

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['firstname'] . "</td>";

echo "<td>" . $row['lastname'] . "</td>";

echo "<td>" . $row['email'] . "</td>";

echo "<td align='center'>" . delete;

echo "</tr>";

}

echo "</table>";

 

mysql_close($con);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/
Share on other sites

echo '<td align="center"><a href="delete.php?id='.$row['id'].'">delete</a>';

 

That's presuming your primary key is called "id"

 

Then inside delete.php use $_GET['id'] to get the parameter you're passing it.

 

Exactly what I was going to say but he beat me to it

alright.  That's awesome and I think right where I need to be.  Now, here comes my "newbieness"  what should my delete.php have in it.  I am just starting to learn some of the different functions, but haven't used the GET one yet.

 

Thanks for your help.

$_GET will get values from the URL

$_POST gets data from a form with method of post

$_REQUEST will get from loads of sources including the above two and cookies

 

Once you have your ID retrieved from $_GET the next step depends if you want to verify with the user that it's the right record to delete.

 

Either way later on you'll need to pass it to a DELETE query like this:

$rowid=intval($_GET['id']);
if (mysql_query("DELETE FROM admin WHERE id=".$rowid." LIMIT 1")) {
  echo 'Deleted';
} else {
  echo 'Not deleted';
}

 

We add "LIMIT 1" as a precaution to make sure we only delete 1 record.

We also use intval() on $_GET because we're expecting a number. Anything other than a number will return 0.

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.