gpt4 book ai didi

php - 表单未从 MySQL 表中删除列数据

转载 作者:行者123 更新时间:2023-11-30 00:39:32 25 4
gpt4 key购买 nike

我有一个 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>&nbsp;</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();
?>

最佳答案

您没有执行 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);

关于php - 表单未从 MySQL 表中删除列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com