[英]Edit and delete button on each row for table data?
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. 但是我不确定如何获取每部电影的ID,以便将其从数据库中删除。 What's the query that I should write?
我应该写什么查询? Do I need to have a separate delete.php file?
我需要一个单独的delete.php文件吗? I´ma very newbie so bear with me:) Below you can see the code that I've done.
我是个新手,请耐心等待:)在下面,您可以看到我完成的代码。
<?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';
?>
if you have to edit and delete on 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 conformation for 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 id on next page using 使用比得到的下一个页面上的ID
$id = $_GET['id'];
hope it will be usefull for you 希望对您有用
I would recommend that you use a form for every row with a delete and edit button and a hidden field with the ID. 我建议您为每行使用一个带有删除和编辑按钮以及带有ID的隐藏字段的表单。 You can then post that form to the right script and determine the action to take there.
然后,您可以将该表单发布到正确的脚本中,并确定要执行的操作。
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. 如果您必须使用链接来删除项目,则至少应使该链接指向另一个确认页面,该页面具有用户必须提交的表单并将其张贴到您的删除脚本中。
If you need a simple and effective solution, use datatables. 如果您需要简单有效的解决方案,请使用数据表。 :) please search in google.
:)请在Google中搜索。 spent some time to study how to implement it, it will save a lot of time in future.
花了一些时间研究如何实现它,它将在将来节省大量时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.