简体   繁体   English

如何在表中使用php删除特定行

[英]How to delete specific rows with php in a table

I was wondering if it is possible to with a button on a table delete that specifc row from the database. 我想知道是否可以用表上的按钮从数据库中删除该特定行。 Here is an example: 这是一个例子:

<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);
      ?>

For example I Have 2 rows in a database, and that creates 2 diferent rows on the HTML table and I want to know if its possible to delete a specif row from database using a button at the end of every row on the HTML page 例如,我在数据库中有2行,并且在HTML表格上创建了​​2条不同的行,我想知道是否有可能使用HTML页面上每行的结尾处的按钮从数据库中删除指定的行

The way to do this would be to use a page.sql file to add specific SQL code, which would either add or drop a table . 这样做的方法是使用page.sql文件添加特定的SQL代码,该代码将adddrop table

For example, any button onclick could then redirect you to the page below, and then redirect back to the main page right away. 例如,任何按钮onclick都可以将您重定向到下面的页面,然后立即重定向回主页。 Then you could have a special page for each row and a separate SQL page for deleting the rows. 然后,您可以为每一行有一个特殊的页面,并为删除这些行提供一个单独的SQL页面。 Good Luck! 祝好运!

name_of_whatever.sql

 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; -- -- Database: `database_name` -- -- -------------------------------------------------------- -- -- Table structure for table `table_name` -- CREATE TABLE IF NOT EXISTS `table_name` ( `id` int(11) NOT NULL AUTO_INCREMENT, `word` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- -- Dumping data for table `table_name` -- INSERT INTO `table_name` (`id`, `word`,) VALUES (1, 'hello'); -- -------------------------------------------------------- 

There are many viable approaches. 有许多可行的方法。 The simplest one I can think of building on top of your code is something like this: 我能想到的最简单的代码如下:

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

Then in your PHP backend code check $_POST["action"] for "deleterow" and execute SQL delete getting the value from $_POST["rowid"] - make sure to properly escape it to avoid SQL injection. 然后在您的PHP后端代码中,检查$_POST["action"]中的“ deleterow”并执行SQL删除,以从$_POST["rowid"]获取值-确保正确地对其进行转义以避免SQL注入。

A more elegant approach that would avoid reloading the page and re-rendering the HTML would be to issue an AJAX call to the backend, but that takes more work. 避免重新加载页面和重新呈现HTML的更优雅的方法是向后端发出AJAX调用,但这需要更多工作。 If this is a simple application that will never have more than 100 or so rows on the page that will never be used by more than a couple of users, it is probably more hassle than it is worth. 如果这是一个简单的应用程序,并且页面上的行数永远不会超过100个左右,并且永远不会被多个用户使用,那么它可能比它值得的麻烦得多。

Make sure you understand how PHP interacts with HTML/Javascript. 确保您了解PHP如何与HTML / Javascript交互。 PHP is executed on the server. PHP在服务器上执行。 HTML is rendered and Javascript is executed on the client. 呈现HTML,并在客户端上执行Javascript。 What you are doing when you write PHP is send HTML/Javascript to the client to render/execute. 在编写PHP时,您正在做的是将HTML / Javascript发送到客户端以进行渲染/执行。 Then when your client submits the form, the PHP is executed on the server to process the submission. 然后,当客户提交表单时,将在服务器上执行PHP以处理提交。 It is PHP on the server that talks to the database. 与数据库对话的是服务器上的PHP。 So in your PHP code you should have something like this : 因此,在您的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(); 

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

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