我有一个PHP表单,允许用户从文件夹images
中删除图像。通过从MySQL表的列photo
调用图像的名称来拉取图像。当用户提交表单时,应该从images
文件夹中删除该图像,并且MySQL表中列photo
中的数据应该将其值清空。
目前,表单会从images
文件夹中删除图像,但不会更改photo
列的值。例如,如果用户要使用表单删除图片dog.jpg
,则photo
列的值将从dog.jpg
更改为。实际图像也将从
images
文件夹中删除。
以下是完整的PHP页面代码:
<?php
// delete an image
if (array_key_exists('delete_file', $_POST)) {
$filename = $_POST['delete_file'];
$identification = $_POST['identify'];
$filename1 = $_POST['deletecolumndata'];
if (file_exists($filename)) {
unlink($filename);
"UPDATE used_trailers ".
"SET photo = '' ".
"WHERE id = $identification" ;
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
// Connect to the database
$dbLink = new mysqli('dsom', 'ssm', 'Ksr', 'ksm');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT * FROM `used_trailers`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b>Title</b></td>
<td><b>Description</b></td>
<td><b>Model</b></td>
<td><b>Make</b></td>
<td><b>Year</b></td>
<td><b>Price</b></td>
<td><b>Photo 1</b></td>
<td><b>Photo 2</b></td>
<td><b>Photo 3</b></td>
<td><b>Photo 4</b></td>
<td><b>Photo 5</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td>{$row['title']}</td>
<td>{$row['description']}</td>
<td>{$row['model']}</td>
<td>{$row['make']}</td>
<td>{$row['year']}</td>
<td>{$row['price']}</td>
<td><img src=images/{$row['photo']}></td>
<form method='post'>
<input type='hidden' value='{$row['id']}' name='identify' />
<input type='hidden' value='images/{$row['photo']}' name='delete_file'/>
<input type='hidden' value='' name='deletecolumndata' />
<input type='submit' value='Delete image' />
</form>
<td><img src=images/{$row['photo1']}></td>
<td><img src=images/{$row['photo2']}></td>
<td><img src=images/{$row['photo3']}></td>
<td><img src=images/{$row['photo4']}></td>
<td><a target='_blank' href='downloadfile.php?id={$row['id']}'>View PDF</a></td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
?>
答案 0 :(得分:0)
您没有执行UPDATE查询。 而不只是像
那样指定字符串 "UPDATE used_trailers ".
"SET photo = '' ".
"WHERE id = $identification" ;
尝试
$sql = "UPDATE used_trailers ".
"SET photo = '' ".
"WHERE id = $identification" ;
$dbLink = new mysqli('dsom', 'ssm', 'Ksr', 'ksm');
$result = $dbLink->query($sql);
答案 1 :(得分:0)
"UPDATE used_trailers ".
"SET photo = '' ".
"WHERE id = $identification" ;
这个查询在空中飞行,但现在什么都不做。您需要建立mysql连接并以查询$sql = 'SELECT * FROM used_trailers';
答案 2 :(得分:0)
因此要么将UPDATE查询更改为DELETE查询(并确保它被执行) 或者更改SELECT查询的WHERE子句
所以要添加一些代码,请执行以下操作:
$updatesql = "UPDATE used_trailers SET photo = '' WHERE id = $identification" ;
$result = $dbLink->query($updatesql);
然后将SELECT查询更改为:
$sql = "SELECT * FROM used_trailers WHERE photo <> ''";
$result = $dbLink->query($sql);
或者将您的更新更改为:
$sql = "DELETE FROM used_trailers WHERE id = $identification" ;
$result = $dbLink->query($sql);
就是这样,没有别的东西可以改变
答案 3 :(得分:0)
试试这个,你错过了执行update
查询。
$updatesql = "UPDATE used_trailers SET photo = '' WHERE id = $identification" ;
$result = $dbLink->query($updatesql);
而不是
"UPDATE used_trailers ".
"SET photo = '' ".
"WHERE id = $identification" ;