1

I setup a little site for my roomate whose an artist, and I set it up where he could use an admin.php page to upload images to the site, and they are added to a MYSQL Database where the main site pulls them from and displays them.

He wants the option to remove images from the same page he adds images, so I wrote a little code to display them again and add a "remove" link to each one. I can't figure out what a good way would be to actually code the rest of it where it actually removes the image when he clicks it.

Can anyone possibly shed some light, urls, etc. that may help to get this finished? Here is what the admin.php looks like thus far...

<html>
<head><title>SethClem.com Image Management</title></head>
<body>

<form enctype="multipart/form-data" action="upload.php" method="POST">
Image File: <input type="file" name="image" /><br />
Description: <input type="text" name ="description" ><br>

<input type="submit" value="upload" />
</form> 

<?php
    include("../database.php");

    // Connects to your Database 
    mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()) ; 
    mysql_select_db($dbname) or die(mysql_error()) ; 

    //Retrieves data from MySQL 
    $data = mysql_query("SELECT * FROM images") or die(mysql_error()); 
?>
    <div style="width:100%; height:105px; border:0 ; padding:5px;">
    <table><tr>
<?php
    //Puts it into an array 
    while($info = mysql_fetch_array( $data )) 
    {
        $image = "../images/".$info['image'];
?>
    <td>
        <img src="<?php echo $image ?>" style="width:191; height:124; border:0px ; float:left;" />
        <a href="_blank">remove</a>
    </td>
<?php
    }
?>
    </tr>
    </table>
    </div>
hakre
  • 193,403
  • 52
  • 435
  • 836

2 Answers2

1
$action = !empty($_GET['action'])?$_GET['action']:false;
$id = !empty($_GET['id'])?$_GET['id']:false;

switch ($action) {
    case 'delete':
        if ($id !== false)
        {
            mysql_query("delete from `images` where `id`='$id' limit 1;");
            //unlink($path_to_image.'/'.$file_name);
        }
    break;
    default:
        echo 'No known action was passed through (Test Message, will be removed)';
}
-1

replace

<a href="_blank">remove</a>

with <a href="http://www.your_domain.com/your_file.php?action=delete&id=12345">remove</a>

now, put this php code imediatly after the db connection:

if( isset( $_GET['action'] ) && ( $_GET['action == 'delete' ) )
{

$id = $_GET['id'];

mysql_query("delete from `images` where `id`='$id' limit 1;");

unlink($path_to_image.'/'.$file_name);

}
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
  • Yeah I actually had to change it a bit, but nonetheless, it explained what I needed to do which was fine. I will post changed code in an answer. –  Jul 11 '12 at 09:03
  • $_GET['action == 'delete') => $_GET['action'] == 'delete' – Itachi Sama Jun 28 '15 at 11:37