php 在php中添加删除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12668141/
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
adding a delete button in php
提问by HurkNburkS
I have created a table of items that are in my DB, it comes out perfectly however I would now like to add a delete button in another column of the data I am outputting.
我在我的数据库中创建了一个项目表,它完美地显示出来,但是我现在想在我输出的数据的另一列中添加一个删除按钮。
However I am just not sure how to do it, I do have a uniqueid for each of the tables so could I use that as the id or what ever you would call it of the button?
但是,我只是不确定该怎么做,我确实为每个表都有一个 uniqueid,所以我可以将其用作 id 还是您将其称为按钮的什么名称?
<?php
//Start session
//... all the connection stuff here
$result = mysql_query("SELECT * FROM feathermattresstoppers");
echo "<table border='1'>
<tr>
<th>name</th>
<th>old price</th>
<th>price</th>
<th>delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['old_price'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>"<!-- how could I add a button here? -->"</td>";
echo "</tr>";
}
echo "</table>";
?>
any help would be appreciated.
任何帮助,将不胜感激。
回答by Ahmet ?z???k
Before your table:
在你的餐桌前:
<form action="" method="post">
and close the form tag after your table.
并在您的表格之后关闭表单标签。
Place this code as your delete button:
将此代码作为删除按钮:
echo '<td><input type="submit" name="deleteItem" value="'.$row['id'].'" /></td>"';
In PHP you should do the following
在 PHP 中,您应该执行以下操作
<?php
if(isset($_POST['deleteItem']) and is_numeric($_POST['deleteItem']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
// $delete = $_POST['deleteItem']
// $sql = "DELETE FROM `tablename` where `id` = '$delete'";
}
?>
回答by ScoRpion
Yes, But you will need to put this table into a form. You can create a button which on click will submit the form (check boxes) of the table rows, and your checkbox id will be the unique id from database row, then you can simply delete the rows with submitted IDs
是的,但是您需要将此表格放入表格中。您可以创建一个按钮,单击该按钮将提交表格行的表单(复选框),您的复选框 ID 将是数据库行中的唯一 ID,然后您可以简单地删除带有提交 ID 的行
回答by arkascha
Two approaches:
两种做法:
1.) you code a small html form for each line in the table. That form contains the lines ID in a hidden input file alongside the button. In your processing php code you get the information that the delete button has been pressed together with the ID of the line to be deleted.
1.) 您为表中的每一行编写一个小的 html 表单。该表单在按钮旁边的隐藏输入文件中包含行 ID。在您处理的 php 代码中,您将获得删除按钮已被按下的信息以及要删除的行的 ID。
2.) you code that in a dynamic way using javascript. Then you simply code the same delete button in all lines of your table. In addition you bind a short javascript function to the 'click' event of all those buttons. If one is clicked your function is called and it can identify the line the clicked button belongs to by looking for the buttons parent element and reading its ID. Then you post back that ID.
2.) 您使用 javascript 以动态方式对其进行编码。然后,您只需在表格的所有行中编写相同的删除按钮。此外,您将一个简短的 javascript 函数绑定到所有这些按钮的“点击”事件。如果一个被点击,你的函数被调用,它可以通过查找按钮父元素并读取其 ID 来识别被点击按钮所属的行。然后你发回那个ID。
回答by Sherin Jose
Try this code,
试试这个代码,
<?php
//codes
echo "<td><form name='frmDelete' action='your delete page here' method='post'><input type='hidden' name='itemid' value='{echo ID here}'><input type='submit' name='dlteBtn' value='delete'></form></td>";
//codes
?>
In the delete page
在删除页面
<?php
if(isset($_POST['dlteBtn'])){
$id=$_POST['itemid'];
//delete query for row with id, $id
}
?>
回答by Arun Killu
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['old_price'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>"<a href="delete.php?<?php echo $row['id']; ?>">delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
and in your delete.php
并在您的 delete.php 中
if(isset($_REQUEST['id']) && is_numeric($_REQUEST['id']))
{
//query for delete.
}
//w3c says you shouldn't use get method for any operation other than retrieval from data base so better method is to use post .
//w3c 说除了从数据库检索之外,您不应该将 get 方法用于任何操作,因此更好的方法是使用 post 。
回答by Sheena
the simplest way would be to enclose each tr in a form and add the unique key as a hidden field.
最简单的方法是将每个 tr 包含在一个表单中,并将唯一键添加为隐藏字段。
while($row = mysql_fetch_array($result))
{
echo '<form action="here_maybe" method="POST" >'
echo '<input name="row_id" type="hidden" value="'.$row['id'].'"/>'
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['old_price'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td><input type="submit" value="Delete" name="delete_command"/></td>";
echo "</tr>";
echo "</form>"
}
回答by Jaicy Joseph
echo '<td><a href="yourpagename.php?delete=1&id='.$row["stud_no"].'"><button type="submit" style="font-weight:bold;" name="delete" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button></a></td>';
You can put your button inside the tag. And cover it will tag and in href attribute you can put your page name in which the url will have the is displayed in it. Like delete=1 is used, so that you can set this button and put &id which shows nd stores the id in it. So that in that same page or next page you can get that id with get or post method. For example i have used id=$row['stud_no'] this will display the id which you have clicked nd id will be displayed in url and then in that page or next page, you can take $id = $_POST['id']; by doing this you can get your stud_no in $id. to see whether you got the proper id , you can echo $id and see it.
您可以将按钮放在标签内。并覆盖它会标记并在 href 属性中,您可以将页面名称放在其中显示 url 的位置。比如使用了delete=1,这样你就可以设置这个按钮并把显示nd的&id放在里面。因此,在同一页面或下一页中,您可以使用 get 或 post 方法获取该 ID。例如,我使用 id=$row['stud_no'] 这将显示您单击的 id nd id 将显示在 url 中,然后在该页面或下一页中,您可以使用 $id = $_POST['id ']; 通过这样做,您可以在$ id 中获取您的stud_no。要查看您是否获得了正确的 id ,您可以 echo $id 并查看它。

