每个字段的删除按钮

时间:2013-08-28 03:33:32

标签: php

我的数据库中有一个项目表,我想要一个删除按钮。

目前它正在运行,但按钮显示了ID号,我希望它只是说"删除"。

我仍然希望它基于joke.ID

删除
$result = mysqli_query($con, "SELECT joke.id, joketext, jokedate, name, email FROM joke INNER JOIN author ON authorid = author.id");

echo "<form action='delete1.php' method='post'>
<table border='1'>
   <tr>
      <th>Joke</th>
      <th>Date</th>
      <th>Name</th>
      <th>Email</th>
      <th>Delete</th>
   </tr>";

   while( $row = mysqli_fetch_array( $result ) ) {
      echo "<tr>";
      echo "<td>" . $row['joketext'] . "</td>";
      echo "<td>" . $row['jokedate'] . "</td>";
      echo "<td>" . $row['name'] . "</td>";
      echo "<td>" . $row['email'] . "</td>";
      echo "<td><input type='submit' name='deleteItem' value='".$row['id']."' /></td>";
      echo "</tr>";
   }
   echo "</table>";
   echo "</form></br>";

   mysqli_close($con);
?>

这是delete1.php

    if ( isset( $_POST['deleteItem'] ) and is_numeric( $_POST['deleteItem'] ) ) {
        // here comes your delete query: use $_POST['deleteItem'] as your id
        mysqli_query($con,"DELETE FROM joke WHERE id='$_POST[deleteItem]'");
    }
    mysqli_close( $con );
    header('Location: joke1.php');
    exit();
?>

我想按一下按钮说&#34;删除&#34;

2 个答案:

答案 0 :(得分:7)

走另一条路。创建一个隐藏的输入元素如下:

echo "<input type='hidden' name='deleteItem' value='".$row['id']."'>";

将现有的更改为

echo "<td><input type='submit' value='Delete' /></td>";

就是这样。

EDIT1:你必须为每个笑话保留一个不同的表格。

while($row = mysqli_fetch_array($result))
{

    echo "<tr>";
    echo "<td>" . $row['joketext'] . "</td>";
    echo "<td>" . $row['jokedate'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>";
    echo "<form action='delete1.php' method='post'>
    echo "<input type='hidden' name='deleteItem' value='".$row['id']."'>";
    echo "<input type='submit' value='Delete' />";
    echo "</form>";
    echo "</td>";
    echo "</tr>";
}

答案 1 :(得分:0)

使用简单的方法

echo "<td><input type='button' value='Delete' onclick='javascript:location.href=\'delete1.php?id=" .$row['id']. "\'' /></td>";

<强> delete1.php

if(isset($_GET['id']) and is_numeric($_GET['id']))