简体   繁体   中英

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

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 .

For example, any button onclick could then redirect you to the page below, and then redirect back to the main page right away. Then you could have a special page for each row and a separate SQL page for deleting the rows. 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.

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

Make sure you understand how PHP interacts with HTML/Javascript. PHP is executed on the server. HTML is rendered and Javascript is executed on the client. What you are doing when you write PHP is send HTML/Javascript to the client to render/execute. Then when your client submits the form, the PHP is executed on the server to process the submission. It is PHP on the server that talks to the database. So in your PHP code you should have something like this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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