从 html 页面删除 mysql 行的最佳方法 - 删除链接和 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2137268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to delete mysql rows from html page - delete link and php
提问by mike varela
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.
我正在使用 while 语句和 PHP 返回表中的所有行。行表示项目列表。我想在每个项目旁边添加删除链接,以便用户可以删除条目。我想弄清楚如何在 PHP 中实现它。任何人都可以帮我写脚本.. 我正在使用程序.. 不是面向对象编程。我认为链接会将用户带到流程页面并返回,但我不会事先知道条目,它都是动态的,并且列表总是在变化。
thanks in advance
提前致谢
回答by RJD22
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:
最佳和保存实践是使用复选框。Google 不会抓取它们,用户不能轻易放入恶意代码,也不会每次删除都刷新页面:
HTML sample:
HTML 示例:
while ($row = mysql_fetch_assoc($items))
{
echo '<input name="delete['.$row['id'].']" type="checkbox">';
}
PHP processing sample:
PHP处理示例:
$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
像这样的东西应该可以很好地完成工作
回答by the_e
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>";
回答by Bug Magnet
Wouldn't using Javascript to delete the record be a possible way to prevent web spiders from following the link?
使用 Javascript 删除记录不是防止网络蜘蛛跟踪链接的可能方法吗?
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...
如果网络蜘蛛不遵循 Javascript 链接,这应该没问题...
回答by John Conde
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.
这是非常直接的。只需使用要删除的行的 ID 作为该 URL 的参数创建一个 URL。然后当点击该链接时,从查询字符串中获取 ID 并执行您的数据库工作。
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.
当然,您需要进行数据验证之类的工作,但这应该会给您一个想法。

