简体   繁体   中英

Form is not Deleting Column Data from MySQL Table

I have a PHP Form that should allow the user to delete an image from the folder images . The image is pulled by calling the name of the image from the MySQL Table's column photo . When the user submits the form, the image should be deleted from the images folder, and the data in the column photo in the MySQL Table should have it's value blanked out.

Currently, the form deletes the image from the images folder, but doesn't change the value of the photo column. For example, if the user were to delete the image dog.jpg using the form, the photo column's value would be changed from dog.jpg to . The actual image would also be deleted from the images folder.

Here is the full PHP page's code:

<?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();
?>

You are not executing the UPDATE query. Instead of just specifying the string like

    "UPDATE used_trailers ".
    "SET photo = '' ".
    "WHERE id = $identification" ;

Try

$sql = "UPDATE used_trailers ".
       "SET photo = '' ".
       "WHERE id = $identification" ;

$dbLink = new mysqli('dsom', 'ssm', 'Ksr', 'ksm');
$result = $dbLink->query($sql);
 "UPDATE used_trailers ".
    "SET photo = '' ".
       "WHERE id = $identification" ;

This query is flying in the air but doing nothing right now. You need to establish mysql connection and query it the way you query $sql = 'SELECT * FROM used_trailers';

i see a lot of mistakes. First your SQL UPDATE query does not get executed. It is just a string. Even if you would execute it, I doubt you would have the desired effect. Because your SELECT query does not have a where clause where it checks for hte photo coloumn.

So either change the UPDATE query into a DELETE query (and make sure it gets executed) or change the WHERE clause of your SELECT query

so to add some code, either do this:

$updatesql = "UPDATE used_trailers SET photo = '' WHERE id = $identification" ;
$result = $dbLink->query($updatesql);

and then change your SELECT query into this:

$sql = "SELECT * FROM used_trailers WHERE photo <> ''";
$result = $dbLink->query($sql);

OR change your UPDATE into:

$sql = "DELETE FROM used_trailers WHERE id = $identification" ;
$result = $dbLink->query($sql);

and thats it, nothing else should get changed

Try this, You have missed to execute the update query.

$updatesql = "UPDATE used_trailers SET photo = '' WHERE id = $identification" ;
$result = $dbLink->query($updatesql);

instead of

 "UPDATE used_trailers ".
"SET photo = '' ".
   "WHERE id = $identification" ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM