声明
如果行不是最后一行,我遇到了有关删除查询的问题。
这是我的前哨职位:How to do auto increment after delete the query(/row) in a PHP file?
因此,我想出了一个新主意来防止出现该问题,该问题我想显示“删除”按钮,并且仅对查询的最后一行执行删除操作。
我所知道的
据我所知,是否要删除PHP文件中的最后一行。
我们可以这样-> $conn->query("ALTER TABLE tablename AUTO_INCREMENT=1;");
show.php (仅表格部分)
<!--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>
<!--Do I need if-else statement to if it is last row or not?-->
<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!-->
</td>
</tr>
<?php endfor ?>
</table>
</div>
<!--End table-->
delete.php
if(isset($_GET['Delete']))
{
$id = mysqli_real_escape_string($conn,$_POST['id']);
$del = $_GET['Delete'];
//$conn->query("DELETE FROM crimenews WHERE crimenews_id=$del");
$stmt = $conn->prepare("DELETE FROM crimenews WHERE crimenews_id=$del");
$stmt->bind_param("sssss",$SESSION['crimenews_id']);
$stmt->execute();
//Delete only last row
//$conn->query("ALTER TABLE crimenews AUTO_INCREMENT=1;");
$_SESSION['message'] = "The news has deleted.";
$_SESSION['msg_type'] = "danger";
$stmt->close();
header("location: front.php");
}
答案 0 :(得分:0)
是的,您可以添加否则来隐藏或显示您的 Delete Button
:
<!--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-->
在上面的代码中,您检查迭代是否等于行结果的最后一个计数。
核实。希望对您有帮助
答案 1 :(得分:0)
使用以下代码更改show.php,以便仅在最后一行显示删除按钮
<!--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中进行以下更改。评论中的更改详情
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");
}
}