0

I'm returning all rows in a table with a while statement and PHP. the rows indicate a list of items. I'd like to include delete links next to each item so users can delete the entries. I'm trying to figure how to go about it in PHP. Can anyone help me write the script.. I'm using procedural.. not OOP. I'm thinking the link takes users to a process page and back, but I'm not gonna be aware of the entries beforehand, it's all dynamic and the list is always changing.

thanks in advance

1
  • What do you have so far, and how doesn't it work? Commented Jan 26, 2010 at 3:23

3 Answers 3

6

Best and save practice is using checkboxes. Google doesn't spider them, users can't put in malicious code easily and it doesn't refresh the page for every delete:

HTML sample:

while ($row = mysql_fetch_assoc($items))
{
    echo '<input name="delete['.$row['id'].']" type="checkbox">';
}

PHP processing sample:

$delete = $_POST['delete'];

foreach($delete as $id = $value)
{
    $id = mysql_real_escape_string($id);
    mysql_query("DELETE FROM table_name WHERE id = $id");
}

Something like this should do the job nicely

Sign up to request clarification or add additional context in comments.

Comments

1

Definitely take a look at Ignacio's comment. Since webspiders are able to follow links...naturally they will hit your delete link and destroy any data you have in there.

I'd recommend making it a tiny form with a submit button instead of a link.

Something along the lines of

echo "<form id='form_$id' method='post'>" ..
      <input type='hidden' name='id' value='$id' /> ..
      <input type='submit' name='submit_$id' value='delete' /> ..
      </form>";

2 Comments

Then you have to look out for malicious users
True, but at least you know who you're dealing with (as long as this area is admin-locked)
0

Wouldn't using Javascript to delete the record be a possible way to prevent web spiders from following the link?

For example, doing something like:

<a href="#" id="delete_link" rel="3">Delete</a>

<script>
  $('#delete_link').click(function() {
    document.location.href = "delete.php?id=" + $(this).attr('rel');
  });
</script>

This should be fine if web spiders do not follow Javascript links...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.