php 表格数据每一行的编辑和删除按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15284380/
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
Edit and delete button on each row for table data?
提问by Lisa
I'm doing a movie library as a school assignment and have list of movies from a database table that I want to have a edit and a delete option next to on each row.
我正在做一个电影库作为学校作业,并且有一个数据库表中的电影列表,我希望在每一行旁边都有一个编辑和删除选项。
I want to use a edit/delete links for it. Like:
我想为它使用编辑/删除链接。喜欢:
"<a href='moviestorage.php?edit=" . $id . "'>Edit</a>"
But I'm not sure how I can fish up the id for each movie so that it's deleted from the database. What's the query that I should write? Do I need to have a separate delete.php file? I′m a very newbie so bear with me:) Below you can see the code that I've done.
但我不确定如何为每部电影获取 ID,以便将其从数据库中删除。我应该写什么查询?我需要有一个单独的 delete.php 文件吗?我是个新手,所以请耐心等待:) 下面你可以看到我完成的代码。
<?php
require 'connect.inc.php';
//This feels incomplete... I′m trying here to fish the ID...
$id = "SELECT id from movies";
$query = "DELETE FROM movies WHERE id='$id'";
$query = "SELECT * FROM movies, categories WHERE movies.genre_id = categories.genre_id";
$result = mysql_query($query);
if (!$result) die ("Database access failed:" .mysql_error()) ;
$rows = mysql_num_rows($result);
echo '<table><tr><th>Title</th><th>Release year</th><th>Genre</th><th>Director</th><th>Update</th><th>Delete</th></tr>';
for ($j = 0 ; $j < $rows ; ++$j) {
echo '<tr><td>' . mysql_result($result,$j,'title') . '</td>' ;
echo '<td>' . mysql_result($result,$j,'release_year') . '</td>' ;
echo '<td>' . mysql_result($result,$j,'genre') . '</td>' ;
echo '<td>' . mysql_result($result,$j,'director') . '</td>' ;
echo '<td>'."<a href='edit_movie.php?edit=" . $id . "'>Edit</a>".'</td>' ;
echo '<td>'."<a href='delete.php?delete=" . $id . "'>Delete</a>".'</td></tr>' ;
}
echo '</table>';
include 'add_movie.php';
?>
回答by dp4solve
if you have to edit and deleteon new page than you can put hyperlink this way
如果您必须在新页面上编辑和删除,则可以这样放置超链接
echo "<a href=\"edit.php?id=".mysql_result($result,$j,'id')."\"><strong>EDIT</strong>";
echo "<a href=\"delete.php?id=".mysql_result($result,$j,'id')."\"><strong>delete</strong>";
and for conformationfor delete you can use this on delete link
和构象进行删除您可以删除链接使用
echo "<a href=\"delete.php?id=".mysql_result($result,$j,'id')."\" onclick=\"return confirm('You want to delete your own account???');\"><strong>delete</strong>";
than get the idon next page using
比使用下一页获取id
$id = $_GET['id'];
hope it will be usefull for you
希望它对你有用
回答by anu g prem
If you need a simple and effective solution, use datatables. :) please search in google. spent some time to study how to implement it, it will save a lot of time in future.
如果您需要一个简单有效的解决方案,请使用数据表。:) 请在谷歌搜索。花一些时间研究如何实现它,将来会节省很多时间。
回答by jeroen
I would recommend that you use a form for every row with a delete and edit button and a hidden field with the ID. You can then post that form to the right script and determine the action to take there.
我建议您为每一行使用一个带有删除和编辑按钮以及带有 ID 的隐藏字段的表单。然后,您可以将该表单发布到正确的脚本并确定要在那里执行的操作。
If you have to use a link to delete an item, at least have the link lead to another confirmation page with a form that the user has to submit and that posts to your delete script.
如果您必须使用链接来删除项目,至少让链接指向另一个确认页面,其中包含用户必须提交并发布到您的删除脚本的表单。

