簡體   English   中英

刪除MySQL和PHP的行按鈕

[英]Delete Row Button for MySQL and PHP

我有一個管理面板,管理員可以在其中查看表中的所有數據,但是我希望他們在每行旁邊都有一個小的刪除按鈕。 每一行都可以由一個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>

有什么辦法可以在每個顯示的行的右側設置刪除按鈕,並使之起作用? 先感謝您!

嘗試這樣的事情:

<?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>;
        ";  
      }
    ?>

在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

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