删除MySQL和PHP的行按钮

时间:2016-02-11 17:32:53

标签: php html mysql

我有一个管理面板,管理员可以在其中查看表格中的所有数据,但我希望他们在每一行旁边都有一个小删除按钮。每行都可以用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>

有没有办法让删除按钮位于每个显示行的右侧,并使其正常运行? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

尝试类似的事情:

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

答案 1 :(得分:0)

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

?>