我为我的艺术家roomate设置了一个小站点,我将它设置在可以使用admin.php页面将图像上传到站点的位置,然后将它们添加到MYSQL数据库中,主站点将它们拉出来来自并显示它们。
他想要选择从他添加图像的同一页面中删除图像,所以我写了一些代码再次显示它们并为每一个添加一个“删除”链接。我无法弄清楚实际编写其余部分的好方法是什么,当它点击它时它实际上删除了图像。
任何人都可以放弃一些可能有助于完成这项工作的灯光,网址等吗?以下是admin.php到目前为止的样子......
<html>
<head><title>SethClem.com Image Management</title></head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Image File: <input type="file" name="image" /><br />
Description: <input type="text" name ="description" ><br>
<input type="submit" value="upload" />
</form>
<?php
include("../database.php");
// Connects to your Database
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()) ;
mysql_select_db($dbname) or die(mysql_error()) ;
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM images") or die(mysql_error());
?>
<div style="width:100%; height:105px; border:0 ; padding:5px;">
<table><tr>
<?php
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$image = "../images/".$info['image'];
?>
<td>
<img src="<?php echo $image ?>" style="width:191; height:124; border:0px ; float:left;" />
<a href="_blank">remove</a>
</td>
<?php
}
?>
</tr>
</table>
</div>
答案 0 :(得分:1)
$action = !empty($_GET['action'])?$_GET['action']:false;
$id = !empty($_GET['id'])?$_GET['id']:false;
switch ($action) {
case 'delete':
if ($id !== false)
{
mysql_query("delete from `images` where `id`='$id' limit 1;");
//unlink($path_to_image.'/'.$file_name);
}
break;
default:
echo 'No known action was passed through (Test Message, will be removed)';
}
答案 1 :(得分:-1)
替换
<a href="_blank">remove</a>
<a href="http://www.your_domain.com/your_file.php?action=delete&id=12345">remove</a>
现在,在db连接之后立即放置这个php代码:
if( isset( $_GET['action'] ) && ( $_GET['action == 'delete' ) )
{
$id = $_GET['id'];
mysql_query("delete from `images` where `id`='$id' limit 1;");
unlink($path_to_image.'/'.$file_name);
}