表单不是从MySQL表中删除列数据

时间:2014-02-19 17:26:50

标签: php mysql sql image

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

4 个答案:

答案 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)

我发现很多错误。 首先,您的SQL UPDATE查询不会被执行。它只是一个字符串。 即使你会执行它,我怀疑你会有所期望的效果。因为SELECT查询没有where子句来检查hte photo coloumn。

因此要么将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" ;