Jump to content

Deleting data from a file && MySQL


Spock

Recommended Posts

Hey guys, first post and I've just returned to PHP after about 4 years of no coding, so be gentle!  :wtf:

 

In a nutshell I'm creating a photo album and I've pretty much got the majority of it complete, apart from a few tweaks and the obvious ongoing development.

 

I'm at the stage now where I need to moderate images being uploaded, so I've made an admin only script which displays the uploaded images with links that say approve and delete.

 

Uploaded images are stored in uploads/ which are left there until i move them to img/, plus the filename is stored in mysql so I can "<img src='../uploads/".$row['filename']."' width='200'>".

 

Now, I would like to make the Approve button move the image from uploads/ to img/ and I'd like the delete button to remove both the entry from MySQL and the file from the uploads folder and I'm not too sure on how to make it work.

 

Here's what I have so far in the mod.php file (mod for moderation)

 

$image = mysql_query("SELECT * FROM images WHERE id");
while($row = mysql_fetch_assoc($image))
{
echo "
<table>
<td>
<tr>
<img src='../uploads/".$row['filename']."' width='200'><br />
</tr>
</td>
<td>
<tr>
<a href=''>Approve</a> <a href=''>Delete</a>
</tr>
</td>
</table>
";
}

 

You'll have to ignore the table tags, I'm still getting used to positioning items on the screen lol.

 

Any clues would be greatly appreciated

 

Live long and prosper.

Link to comment
Share on other sites

Okay excellent, i've managed to get both the file and the mysql entry to delete, now for approving I was thinking of using UPDATE, but I'm wondering how I do so in such a way that a single image would have a value changed in mysql rather than them all.

 

I've tried the following but it doesn't work, any ideas?

 

$appfile=$_GET['filename'];
$sql = mysql_query("UPDATE images WHERE filename='$appfile' SET approved='1'");

if ($sql)
{	
echo "Approved!";
echo "<br />";
echo "<a href='mod.php'>Back</a>";
}
else
{
echo "Error.";
echo "<br />";
echo "<a href='mod.php'>Back</a>";
}

Link to comment
Share on other sites

1. echo $appfile to be sure it contains the value you expect.

 

IF it does have the correct value

change this...

$sql = mysql_query("UPDATE images WHERE filename='$appfile' SET approved='1'");

to this...

$sql = mysql_query("UPDATE images SET approved=1 WHERE filename='$appfile'");

Link to comment
Share on other sites

Thank you ever so much litebearer, you've been a great help.

 

The final thing I'm struggling with now is displaying the images that have an approved value of 1 instead of 0

 

I thought I was being clever by using an if statement, but that just basically said

 

 

if approved equals 1, then display everything

 

 

So i need to rethink it

 

 

		//Display data
	$get = mysql_query("SELECT * FROM images LIMIT $start, $per_page");
	$approved = mysql_query("SELECT * FROM images WHERE approved");
	$fetchapproved = mysql_fetch_assoc($approved);
	while($row = mysql_fetch_assoc($get))
	{
		// get data
		$filename = $row['filename'];

		echo "<div align='center'><img src='../uploads/".$filename."' / width='600px'></div>";
	}

Link to comment
Share on other sites

That just sets the variable though, how does it filter them out?

 

Should I reuse the if statement around the whole statement?

 

No, it says to get all(*) from images where approved is equal to one. That's what you said you wanted

 

 

Link to comment
Share on other sites

Ahhhhhhh. You need pagination...

 

There's plenty of tutorials here at phpfreaks and also on google.

They'll be easy to find. Basically you set an offset and use LIMIT to return what you want.

 

Look into it

Link to comment
Share on other sites

That sort of works, it only displays the images with approved set to 1, however you can still navigate for the images it doesn't display...I think I might do it the way I originally thought and move the file to a different folder, it'll probably be simpler lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.