简体   繁体   English

在php中添加删除按钮

[英]adding a delete button in php

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? 但是我只是不确定如何做到这一点,我确实为每个表都有一个唯一的ID,所以我可以使用它作为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. 任何帮助,将不胜感激。

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'"; 
}
?> 

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的行

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

Two approaches: 两种方法:

1.) you code a small html form for each line in the table. 1.)您为表中的每一行编写一个小的html表单。 That form contains the lines ID in a hidden input file alongside the button. 该表单在按钮旁边的隐藏输入文件中包含行ID。 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. 在您处理PHP代码时,您将获得已删除删除按钮的信息以及要删除的行的ID。

2.) you code that in a dynamic way using javascript. 2.)您使用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. 此外,您将一个简短的javascript函数绑定到所有这些按钮的“click”事件。 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. 如果单击一个,则调用您的函数,它可以通过查找父元素按钮并读取其ID来识别单击按钮所属的行。 Then you post back that ID. 然后你回发那个ID。

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
 }
?>
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。

    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. 并覆盖它将标记,并在href属性中,您可以将您的页面名称显示在其中。 Like delete=1 is used, so that you can set this button and put &id which shows nd stores the id in it. 就像使用了delete = 1一样,你可以设置这个按钮并输入显示nd的id和id来存储id。 So that in that same page or next page you can get that id with get or post method. 因此,在同一页面或下一页中,您可以使用get或post方法获取该ID。 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']; 例如,我使用了id = $ row ['stud_no'],这将显示你点击的id nd id将显示在url中,然后在该页面或下一页中,你可以获取$ id = $ _POST ['id “]; by doing this you can get your stud_no in $id. 通过这样做,您可以获得$ id中的stud_no。 to see whether you got the proper id , you can echo $id and see it. 要查看您是否拥有正确的ID,您可以回显$ id并查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM