简体   繁体   English

删除MySQL和PHP的行按钮

[英]Delete Row Button for MySQL and PHP

I have an admin panel where administrators can view all the data in a table, however I would like them to have a small delete button next to each row. 我有一个管理面板,管理员可以在其中查看表中的所有数据,但是我希望他们在每行旁边都有一个小的删除按钮。 Each row can be defined by an ID, however I am not sure about the code behind it. 每一行都可以由一个ID定义,但是我不确定它后面的代码。

        <!-- Requests -->
        <div class="panel panel-<?php echo $PColor; ?>">
        <table class="table table-striped table-requests">
            <thead class="table-requests">
              <tr>
                <th>Req ID</th>
                <th><?php echo "Song Name"; ?></th>
                <th><?php echo "Requested By"; ?></th>
                <th><?php echo "Comments (If any)"; ?></th>
                <th><?php echo "Time"; ?></th>
              </tr>
            </thead>
            <tbody>
<?php

      // Select Requests
      $SelectRequests = $db->query("SELECT * FROM `requests`");


      // Print Output
      foreach($SelectRequests as $PrintRequests)
      {
        echo 
        "
          <tr>
            <td><b>" . $PrintRequests['ID'] . "</b></td>
            <td>" . $PrintRequests['song'] . "</td>
            <td>" . $PrintRequests['name'] . "</td>
            <td>" . $PrintRequests['dedicated'] . "</td>
            <td>" . $PrintRequests['time'] . "</td>
        ";  
      }
    ?>
        </tbody>
    </table>
    </div>
</div>

Is there any way to have the delete button to the right of each displayed row, and make it functional? 有什么办法可以在每个显示的行的右侧设置删除按钮,并使之起作用? Thank you in advance! 先感谢您!

try something like that : 尝试这样的事情:

<?php

      // Select Requests
      $SelectRequests = $db->query("SELECT * FROM `requests`");

      // Print Output
      foreach($SelectRequests as $PrintRequests)
      {
        echo 
        "
          <tr>
            <td><b>" . $PrintRequests['ID'] . "</b></td>
            <td>" . $PrintRequests['song'] . "</td>
            <td>" . $PrintRequests['name'] . "</td>
            <td>" . $PrintRequests['dedicated'] . "</td>
            <td>" . $PrintRequests['time'] . "</td>
            <td> <a href='delete.php?pid=" . $PrintRequests['ID'] . "'> Delete</a> </td>;
        ";  
      }
    ?>

In delete.php 在delete.php中

 <?php
 // Connexion to database..
    $Query="delete from requests where id=".$_REQUEST['pid'];
    if(!mysqli_query($Conection,$Query)) {echo "<i>Error !</i>";}
echo '<script language="javascript"> document.location="Your_previous_page.php";</script>'; 
    ?>
<!-- Lets call this file index.php -->
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<form action='deleteRecord.php' method='GET'>
<table class="table table-striped table-requests">
    <thead class="table-requests">
        <tr>
            <th>Req ID</th>
            <th><?php echo "Song Name"; ?></th>
            <th><?php echo "Requested By"; ?></th>
            <th><?php echo "Comments (If any)"; ?></th>
            <th><?php echo "Time"; ?></th>
            <!-- Add this for the table header -->
            <th><?php echo "Delete"; ?></th>
        </tr>
    </thead>
        <tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");

// Print Output
foreach($SelectRequests as $PrintRequests){
    echo 
        "<tr>
            <td><b>" . $PrintRequests['ID'] . "</b></td>
            <td>" . $PrintRequests['song'] . "</td>
            <td>" . $PrintRequests['name'] . "</td>
            <td>" . $PrintRequests['dedicated'] . "</td>
            <td>" . $PrintRequests['time'] . "</td>
            <td><input type='checkbox' value ='" . $PrintRequests['ID'] . "' name='removal[]'></td>
        </tr>";  
  }
?>
    </tbody>
</table>
<input type="submit" value="Submit">
</form>
</div>

deleteRecord.php deleteRecord.php

<?php
//Assume you can connect to DB
$removals = $_GET['removal'];
if(!empty($removals)){
    if(count($removals) > 1){
        $r = implode(', ', $removals);
        $query="DELETE from requests where ID IN (". $r . ")";
    } else {
        $query="DELETE from requests where ID = ". $removals[0];    
    }
    //Commit to db
} else {
    echo "Nothing Selected";
    //Handle an error if needed
}
//redirect

?>

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

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