在php中添加删除按钮

Hur*_*rkS 3 php mysql

我已经创建了一个包含在我的数据库中的项目表,它完美地出来了但是我现在想在我输出的数据的另一列中添加一个删除按钮.

但是我只是不确定如何做到这一点,我确实为每个表都有一个唯一的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>";

?>
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Ahm*_*şık 13

在你的桌子前:

<form action="" method="post">
Run Code Online (Sandbox Code Playgroud)

并关闭表格后的表格标签.

将此代码作为删除按钮:

echo '<td><input type="submit" name="deleteItem" value="'.$row['id'].'" /></td>"';
Run Code Online (Sandbox Code Playgroud)

在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'"; 
}
?> 
Run Code Online (Sandbox Code Playgroud)