我为室友的艺术家设置了一个小网站,他可以使用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>
$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)';
}
替换
<a href="_blank">remove</a>
带<a href="http://www.your_domain.com/your_file.php?action=delete&id=12345">remove</a>
现在,把这个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);
}