Best way to delete mysql rows from html page delete link and php - stack overflow

3

Click here to load reader

description

465465464445

Transcript of Best way to delete mysql rows from html page delete link and php - stack overflow

Page 1: Best way to delete mysql rows from html page   delete link and php - stack overflow

mike varela

12 3

4 Answers

espais

1,524 1 5 18

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

php mysql html

asked Jan 26 '10 at 3:01

50% accept rate

What do you have so far, and how doesn't it work? – Ignacio Vazquez-Abrams Jan 26 '10 at 3:23

feedback

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>";

answered Jan 26 '10 at 8:58

Then you have to look out for malicious users – Charlie Somerville Jan 26 '10 at 9:02

True, but at least you know who you're dealing with (as long as this area is admin-locked) – espais Jan 26 '10 at

13:57

feedback

Best way to delete mysql rows from html page - delete link and php

×Welcome to Q&A for professional and enthusiast programmers — check out the FAQ!

Best way to delete mysql rows from html page - delete link a... http://stackoverflow.com/questions/2137268/best-way-to-dele...

1 of 3 16-Jan-12 10:18 PM

Page 2: Best way to delete mysql rows from html page   delete link and php - stack overflow

RJD22

1,682 2 11

John Conde

9,886 5 11 27

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

answered Jan 26 '10 at 12:05

feedback

This is pretty straight forward. Just create a URL with the ID of the row you wish to delete as a parameter

of that URL. Then when that link is clicked get the ID from the query string and do your database work.

To create the link:

<?php

// Do query here

while ($row = mysql_fetch_assoc($resource_id))

{

echo "<a href="delete.php?id={$row['id']}">Delete row</a>;

}

?>

To process the delete request:

<?php

$id = $_GET['id'];

mysql_query("DELETE FROM table_name WHERE id = {$id}");

?>

Naturally you need to do data validation and stuff but this should give you the idea.

answered Jan 26 '10 at 3:25

1 Sure... and watch as a webspider wipes out your database. – Ignacio Vazquez-Abrams Jan 26 '10 at 3:26

This is just a raw example. That's why I put the disclaimer at the end. – John Conde Jan 26 '10 at 12:42

delete.php should check (server-side) that the user has permission to do the deleting, that'll prevent web spiders

doing any damage. – vincebowdren Feb 1 '10 at 22:35

Was this post useful to you? Yes No

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:

×Welcome to Q&A for professional and enthusiast programmers — check out the FAQ!

Best way to delete mysql rows from html page - delete link a... http://stackoverflow.com/questions/2137268/best-way-to-dele...

2 of 3 16-Jan-12 10:18 PM

Page 3: Best way to delete mysql rows from html page   delete link and php - stack overflow

Bug Magnet

545 3 11

<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...

edited Jul 7 '11 at 9:06 answered Jul 7 '11 at 7:43

feedback

Not the answer you're looking for? Browse other questions tagged php mysql html

or ask your own question.

question feed

×Welcome to Q&A for professional and enthusiast programmers — check out the FAQ!

Best way to delete mysql rows from html page - delete link a... http://stackoverflow.com/questions/2137268/best-way-to-dele...

3 of 3 16-Jan-12 10:18 PM