首页 > 解决方案 > 如何用php删除表中的特定行

问题描述

我想知道是否可以使用表上的按钮从数据库中删除该特定行。这是一个例子:

<thead>
   <tr>
      <th>Identity</th>
      <th>Name</th>
      <th>Stuff</th>
      <th>Action(Delete)</th>
   </tr>
</thead>
<tbody>
   <?php
      $con=mysqli_connect("","","","");
      // Check connection
      if (mysqli_connect_errno())
      {
      echo "Error " . mysqli_connect_error();
      } 

      $result = mysqli_query($con,"SELECT * FROM sm_admins");

      while($row = mysqli_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['identity'] . "</td>";
      echo "<td>" . $row['name'] . "</td>";
                                          switch ($row['level']) {
                                              case "hello" : echo "<td>Nice Try</td>";
                                                  break;
                                              case "bye" : echo "<td>Row in a row</td>";
                                                  break;
                                              case "Cake" : echo "<td>Lemon</td>";
                                                  break;
                                              case "ao" : echo "<td>Key</td>";
                                                  break;
                                              case "as" : echo "<td>Charger</td>";
                                                  break;

                                          echo "<td> <button class='btn btn-red btn-icon-only' onclick='deleterow(" . $row['identity'] . ")> <i class='fa fa-times'></i> </button></td>";
                                        }
      echo "</tr>";
      }
      echo "</table>";

      mysqli_close($con);
      ?>

例如,我在数据库中有 2 行,并且在 HTML 表上创建了 2 个不同的行,我想知道是否可以使用 HTML 页面上每一行末尾的按钮从数据库中删除特定行

标签: phphtmlmysqlsqlphpmyadmin

解决方案


有许多可行的方法。我能想到的在您的代码之上构建的最简单的是这样的:

<script>
function deleterow(rowid)
{
  var f = document.forms['theform'];
  f.rowid.value = rowid;
  f.submit();
}
</script>
<form name="theform" method="post">
<input type="hidden" name="rowid"/>
<input type="hidden" name="action" value="deleterow"/>
</form>

然后在您的 PHP 后端代码中检查$_POST["action"]“deleterow”并执行 SQL 删除从中获取值$_POST["rowid"]- 确保正确转义它以避免 SQL 注入。

避免重新加载页面和重新呈现 HTML 的更优雅的方法是向后端发出 AJAX 调用,但这需要更多的工作。如果这是一个简单的应用程序,页面上的行数永远不会超过 100 行,并且不会被超过几个用户使用,那么它可能比它的价值更麻烦。

确保您了解 PHP 如何与 HTML/Javascript 交互。PHP 在服务器上执行。呈现 HTML 并在客户端上执行 Javascript。当您编写 PHP 时,您正在做的是将 HTML/Javascript 发送到客户端以呈现/执行。然后当您的客户端提交表单时,在服务器上执行 PHP 以处理提交。与数据库对话的是服务器上的 PHP。所以在你的 PHP 代码中你应该有这样的东西:

function handle_action()
{
  $action = isset($_POST["action"]) ? $_POST["action"] : "";
  switch ($_POST["action"])
  {
    case "deleterow":
      handle_delete();
      load_data();
      break;
    default:
      load_data();
  }
}

function handle_delete()
{
  $rowid = isset($_POST["rowid"]) ? $_POST["rowid"] : "";
  if (!$rowid) return;
  mysqli_query("delete from sms_admins where identity = '".
mysqli_escape_string($rowid)."'");
}

function load_data()
{
  // put everything in your example here except for mysqli_connect
}

$con=mysqli_connect("","","","");
 // Check connection
if (mysqli_connect_errno())
{
    echo "Error " . mysqli_connect_error();
}

handle_action(); 

推荐阅读