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
https://forums.phpfreaks.com/topic/230137-deleting-data-from-a-file-mysql/
Share on other sites

http://webxadmin.free.fr/article/php-move-file-211.php

 

However, really no need to move the images; just add a field  to the db table, a field that is 0 or 1; 0 means the image is awaiting 'approval' so don't display to 'public',  1 means the image has been 'approved'. all the images can be in the same folder

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>";
}

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'");

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>";
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.