Php 如何使“删除”按钮仅在表的最后一行显示和执行操作?
声明 之后,我遇到了一个问题:如果该行不是最后一行,则删除该查询 这是我早期的帖子: 因此,我提出了一个新的想法来防止这个问题,我想显示delete按钮,只对查询的最后一行执行delete操作 我所知道的 据我所知,如果我想删除PHP文件中的最后一行 我们可以这样做->Php 如何使“删除”按钮仅在表的最后一行显示和执行操作?,php,html,Php,Html,声明 之后,我遇到了一个问题:如果该行不是最后一行,则删除该查询 这是我早期的帖子: 因此,我提出了一个新的想法来防止这个问题,我想显示delete按钮,只对查询的最后一行执行delete操作 我所知道的 据我所知,如果我想删除PHP文件中的最后一行 我们可以这样做->$conn->query(“altertabletablename AUTO_INCREMENT=1;”) show.php(仅表格部分) 是,您可以添加if-else以隐藏或显示您的删除按钮: <!--Table-->
$conn->query(“altertabletablename AUTO_INCREMENT=1;”)代码>
show.php(仅表格部分)
是,您可以添加if-else以隐藏或显示您的删除按钮
:
<!--Table-->
<div class="container">
<table class="table table-hover" border='2' align='center' style='color: white'>
<!--Header-->
<thead>
<tr>
<th><center>ID</center></th>
<th><center>Type</center></th>
<th><center>URL</center></th>
<th><center>Date</center></th>
<th><center>Lattitude</center></th>
<th><center>Longitude</center></th>
<th colspan="2"><center>Action</center></th>
</tr>
</thead>
<!--Queries-->
<?php
//$rowCount = count($results->data); //or mysqli_num_rows($results)
for($i=0;$i<count($results->data);$i++):
$news = $results->data[$i];
?>
<tr>
<td><b><?php echo $news['crimenews_id'];?></b></td>
<td><?php echo $news['crimenews_cat'];?></td>
<td><?php echo $news['crimenews_url'];?></td>
<td><?php echo $news['crimenews_datetime'];?></td>
<td><?php echo $news['crimenews_locationLat'];?></td>
<td><?php echo $news['crimenews_locationLong'];?></td>
<td>
<a href="front.php?Edit=<?php echo $news['crimenews_id'];?>" class="btn btn-light"><b>Edit     <i class="far fa-edit"></i></b></a>
<!--Do I need if-else statement to if it is last row or not?-->
<?php
if($i===(count($results->data)-1)){?>
<a href="process.php?Delete=<?php echo $news['crimenews_id'];?>" class="btn btn-danger"><b>Delete <i class="far fa-trash-alt"></i></b></a> <!--Delete button here!-->
<?php }?>
</td>
</tr>
<?php endfor ?>
</table>
</div>
<!--End table-->
身份证件
类型
统一资源定位地址
日期
格子
经度
行动
在上面的代码中,检查迭代是否等于行结果的最后计数。
检查一下。我希望它有帮助是的,您可以添加,否则隐藏或显示您的删除按钮
:
<!--Table-->
<div class="container">
<table class="table table-hover" border='2' align='center' style='color: white'>
<!--Header-->
<thead>
<tr>
<th><center>ID</center></th>
<th><center>Type</center></th>
<th><center>URL</center></th>
<th><center>Date</center></th>
<th><center>Lattitude</center></th>
<th><center>Longitude</center></th>
<th colspan="2"><center>Action</center></th>
</tr>
</thead>
<!--Queries-->
<?php
//$rowCount = count($results->data); //or mysqli_num_rows($results)
for($i=0;$i<count($results->data);$i++):
$news = $results->data[$i];
?>
<tr>
<td><b><?php echo $news['crimenews_id'];?></b></td>
<td><?php echo $news['crimenews_cat'];?></td>
<td><?php echo $news['crimenews_url'];?></td>
<td><?php echo $news['crimenews_datetime'];?></td>
<td><?php echo $news['crimenews_locationLat'];?></td>
<td><?php echo $news['crimenews_locationLong'];?></td>
<td>
<a href="front.php?Edit=<?php echo $news['crimenews_id'];?>" class="btn btn-light"><b>Edit     <i class="far fa-edit"></i></b></a>
<!--Do I need if-else statement to if it is last row or not?-->
<?php
if($i===(count($results->data)-1)){?>
<a href="process.php?Delete=<?php echo $news['crimenews_id'];?>" class="btn btn-danger"><b>Delete <i class="far fa-trash-alt"></i></b></a> <!--Delete button here!-->
<?php }?>
</td>
</tr>
<?php endfor ?>
</table>
</div>
<!--End table-->
身份证件
类型
统一资源定位地址
日期
格子
经度
行动
在上面的代码中,检查迭代是否等于行结果的最后计数。
检查一下。我希望下面的代码有助于更改show.php,以便只在最后一行显示delete按钮
<!--Table-->
<div class="container">
<table class="table table-hover" border='2' align='center' style='color: white'>
<!--Header-->
<thead>
<tr>
<th><center>ID</center></th>
<th><center>Type</center></th>
<th><center>URL</center></th>
<th><center>Date</center></th>
<th><center>Lattitude</center></th>
<th><center>Longitude</center></th>
<th colspan="2"><center>Action</center></th>
</tr>
</thead>
<!--Queries-->
<?php
for($i=0;$i<count($results->data);$i++):
$news = $results->data[$i];
?>
<tr>
<td><b><?php echo $news['crimenews_id'];?></b></td>
<td><?php echo $news['crimenews_cat'];?></td>
<td><?php echo $news['crimenews_url'];?></td>
<td><?php echo $news['crimenews_datetime'];?></td>
<td><?php echo $news['crimenews_locationLat'];?></td>
<td><?php echo $news['crimenews_locationLong'];?></td>
<td>
<a href="front.php?Edit=<?php echo $news['crimenews_id'];?>" class="btn btn-light"><b>Edit     <i class="far fa-edit"></i></b></a>
<?php if($i==(count($results->data)-1)){ ?>
<a href="delete.php?Delete=<?php echo $news['crimenews_id'];?>" class="btn btn-danger"><b>Delete <i class="far fa-trash-alt"></i></b></a> <!-- your file is delete.php not process.php -->
<?php } ?>
</td>
</tr>
<?php endfor ?>
</table>
</div>
<!--End table-->
使用下面的代码更改show.php,以便仅在最后一行显示delete按钮
<!--Table-->
<div class="container">
<table class="table table-hover" border='2' align='center' style='color: white'>
<!--Header-->
<thead>
<tr>
<th><center>ID</center></th>
<th><center>Type</center></th>
<th><center>URL</center></th>
<th><center>Date</center></th>
<th><center>Lattitude</center></th>
<th><center>Longitude</center></th>
<th colspan="2"><center>Action</center></th>
</tr>
</thead>
<!--Queries-->
<?php
for($i=0;$i<count($results->data);$i++):
$news = $results->data[$i];
?>
<tr>
<td><b><?php echo $news['crimenews_id'];?></b></td>
<td><?php echo $news['crimenews_cat'];?></td>
<td><?php echo $news['crimenews_url'];?></td>
<td><?php echo $news['crimenews_datetime'];?></td>
<td><?php echo $news['crimenews_locationLat'];?></td>
<td><?php echo $news['crimenews_locationLong'];?></td>
<td>
<a href="front.php?Edit=<?php echo $news['crimenews_id'];?>" class="btn btn-light"><b>Edit     <i class="far fa-edit"></i></b></a>
<?php if($i==(count($results->data)-1)){ ?>
<a href="delete.php?Delete=<?php echo $news['crimenews_id'];?>" class="btn btn-danger"><b>Delete <i class="far fa-trash-alt"></i></b></a> <!-- your file is delete.php not process.php -->
<?php } ?>
</td>
</tr>
<?php endfor ?>
</table>
</div>
<!--End table-->
我必须在delete.php
中更改任何内容吗?或者只在show.php
中进行修改,我已经添加了它,但最后一行中没有显示删除按钮。您是否尝试了或部分//或mysqli_num_行($results)
错误警告:mysqli_num_行()预期参数1为mysqli_结果,给定对象
-->mysqli_num行($results)中有错误代码>让我们来。我必须在delete.php
中更改任何内容吗?或者只在show.php
中进行修改,我已经添加了它,但最后一行中没有显示删除按钮。您是否尝试了或部分//或mysqli_num_行($results)
错误警告:mysqli_num_行()预期参数1为mysqli_结果,给定对象
-->mysqli_num行($results)中有错误代码>让我们。
if(isset($_GET['Delete']))
{
// $id = mysqli_real_escape_string($conn,$_POST['id']); this is not needed and will through an error as $_POST['id'] is not set
$del = $_GET['Delete'];
$stmt = $conn->prepare("DELETE FROM crimenews WHERE crimenews_id=?"); //change $del to ?
$stmt->bind_param("i",$del); //change ssss to i as I guess the id is an int and bind the correct parameter
if ($stmt->execute()) {
$_SESSION['message'] = "The news has deleted.";
$_SESSION['msg_type'] = "danger";
$stmt->close();
header("location: front.php");
}
}