php: How to delete image from server with single button click?

Hello, fellow PHP experts. I have a script that just isn’t working for me. It consists of a remove button that
posts to the same page when clicked. And then if(isset($_POST[“submit”])), it will try to delete and unlink()
the image from the server, then delete the image path from the database. But what happens when I test it out?

[php]

<?php session_start(); $userid = $_GET["id"]; try { $conn = new PDO("mysql:dbname=users;host=localhost", "register", "mysql" ); $sql = $conn->prepare("SELECT * FROM uploads WHERE id = ? "); $sql->execute(array($userid)); $sql->execute(); while ($result = $sql -> fetch()) { ?>

<?php echo $result ["Title"]; ?>

<?php echo $result ["Description"]; ?>

<img src = “http://68.192.202.0:87/uploads/<?php echo $result ["Directory"]; ?>” height = “500” width = “600” />

<?php if ($userid == $_SESSION["id"]) { ?> <?php } if (isset($_POST["delete"])) { $file = $result ["Directory"]; unlink("uploads/" . $file); $msql = $conn->prepare("DELETE FROM uploads WHERE Directory = ?"); $msql->execute(array($file)); $msql->execute(); } } } catch(PDOException $e) { echo $e->getMessage(); $conn = null; } ?>

[/php]

can you check to see whats wrong with my code? thanks so much

first off these are wrong
both of them need either single quotesaround like this
[php]

$sql = $conn->prepare("SELECT * FROM uploads WHERE id = ‘?’ ");

$msql = $conn->prepare(“DELETE FROM uploads WHERE Directory = ‘?’”);

[/php]
but why is there a question mark there

you need information in it that tells it what the directory is
is the picture in the name in the database or how is the database set up you know give a few things like that

My database table has the path to the file. For example, http:mysite.com/uploads/hello.jpeg;
Can someone give me an example of a form and a button that remove’s photos based on the id
and path, one click = 1 image removed? thanks

<?php echo $result ["Title"]; ?>

<?php echo $result ["Description"]; ?>

<img src = “http://68.192.202.0:87/uploads/<?php echo $result ["id"]; ?>” height = “500” width = “600” />

and

$msql = $conn->prepare("DELETE FROM uploads WHERE id ={$_REQUEST[id]}");

not tested but a general durection

Why are you deleting the user’s id? I need it to delete the path from the database. My database does not have image ids for each image. Just a path.

not deleting the user id’s you are looking for the picture with the id of that table

the id # of the picture in that table

My pictures do not have id’s they only have paths. im deleteing from the uploads table where the
path = $path… get it?

ok let me start from the begining do you know what every piece of this code does

Yes. First, if the user id matches with that profile’s id, it will display a delete button along with every image.
Then, it fetches the directory from each image, then it unlink(), then deletes that row from the table where the directory = the directory selected. but the problem is in the sql delete statement, i beleive. for some reason, it completely disregards fetching certain directory that is selected. it just deletes the first row in the table. can you please revise the code for me? thanks

my apologies first i dont use pdo

and i ask is there a reason you are using pdo any specific

i would code it differently but if that is your preference

I use pdo so it escapes the querys through the parameterized queries, which explains the question marks. if you coded in mysql_query language, i would understand that just as well. i can easily translate between sql and pdo.

well the problem with fetching a directory is all the photos are in that directory right

No I’m fetching specifically the file name.
So $dierctory = “uploadsfolder/” . $filename

$filename = “hello.jpeg”

I query the database for the filename hello.jpeg then connect it into the uploads/ folder to combine into
a path to unlink() it

ok lets see if this mysql guy can figure out a pdo

what exactly is it doing when you call that code

It will remove only the first photo in the database. so its just telling the query to delete the first row. now the row with tyhe directory selected. why is this?

have you tried like the mysql while loop to fetch all the instances of the table columns including the directory

or [php while($row = $stmt->fetch()){ …[/php]

type stuff because you need to get all the ones that have that directory right

or [php] while($row = $stmt->fetch()){ …[/php]

type stuff because you need to get all the ones that have that directory right

sorry bout that

If you mean, the while() statement, like while $result = $sql -> fetch(), then yes. I fetched
all row data into the $result variable. I retreived it like $result [“Directory”]. When i queried with it,
it didnt give the directory of each photo like intended for, but it instead deletes the first row in the database.

How do I get the directory of each image into a variable, so i can use it for deleting and unlink()?

Sponsor our Newsletter | Privacy Policy | Terms of Service