Php 如何在图像上创建删除按钮并添加其功能
我在编码方面比较新,所以我需要你的帮助。我希望你能帮助我 我创建了一个数据库并将图像从数据库检索到PHP文件,当我尝试添加删除按钮时出现了一个错误,该按钮将从数据库中删除图像。下面是我的代码,请帮助我添加删除按钮及其功能:Php 如何在图像上创建删除按钮并添加其功能,php,mysql,json,Php,Mysql,Json,我在编码方面比较新,所以我需要你的帮助。我希望你能帮助我 我创建了一个数据库并将图像从数据库检索到PHP文件,当我尝试添加删除按钮时出现了一个错误,该按钮将从数据库中删除图像。下面是我的代码,请帮助我添加删除按钮及其功能: <section class="content"> <div class="container-fluid"> <div class="gallery"> <div class="col-
<section class="content">
<div class="container-fluid">
<div class="gallery">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
GALLERY
<!--<small>All pictures taken from <a href="https://unsplash.com/" target="_blank">unsplash.com</a></small>-->
</h2>
<hr/>
<div class="body">
<div id="aniimated-thumbnials" class="list-unstyled row clearfix">
<?php
//Include database configuration file
include('db_upload_dashboard.php');
//get images from database
$query = $db->query("SELECT * FROM upload_img ORDER BY uploaded_on DESC");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
$imageThumbURL = 'images/thumb/'.$row["file_name"];
$imageURL = 'images/'.$row["file_name"];
?>
画廊
//请帮我在这里添加删除按钮
<button id="delete"> Delete
<a href="<?php echo $imageURL; ?>" data-fancybox="group" data-caption="<?php echo $row["title"]; ?>" >
<img src="<?php echo $imageThumbURL; ?>" alt="" />
</a>
</button>
<?php }
} ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
删除
HTML禁止在按钮内嵌套链接
从编写有效的语义HTML开始,说明您的意思
您希望发出更改数据库的HTTP请求。这意味着你需要一个POST请求。这意味着你需要一张表格。从这里开始
<form method="POST" action="/delete-image.php">
</form>
您需要一个触发操作的按钮
<form method="POST" action="/delete-image.php">
<button>Delete</button> <!-- submit is the default type of button -->
</form>
删除
您需要传递描述要删除的图像的数据:
<form method="POST" action="/delete-image.php">
<button name="delete" value="<?php echo htmlspecialchars($row['id']); ?>">
Delete
</button>
</form>
你能不能编辑一下,这样我们就可以得到你试图添加按钮时出错的代码,而不是没有它的工作代码?添加错误本身也会有所帮助。这将使我们更好地了解您试图做什么,您将得到什么错误?
<form method="POST" action="/delete-image.php">
<button name="delete" value="<?php echo htmlspecialchars($row['id']); ?>">
<img src="<?php echo htmlspecialchars($imageThumbURL)" alt="">
</button>
</form>
<?php
if (!isset($_POST['delete'])) {
show_an_error();
exit();
}
$row_id_to_delete = $_POST['delete'];
# Database query code left as an exercise to the reader