我有一个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/

10-14 11:37
查看更多