-6ren">
gpt4 book ai didi

php - MySQL 和 PHP 的删除行按钮

转载 作者:行者123 更新时间:2023-11-29 21:29:51 24 4
gpt4 key购买 nike

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

有没有办法让删除按钮位于每个显示行的右侧,并使其起作用?预先感谢您!

最佳答案

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

删除记录.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

?>

关于php - MySQL 和 PHP 的删除行按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com