Jump to content

How to alter the code so it's in one page delete-pictures or redirect via $_GET


The_Dude_1978

Recommended Posts

Hi guy's,

 

I've figure out most of the code (propably simple to you) for my admin page. But i'm stuck with the deleting of pictures because when it goes to deletefile.php it say's it can't find the path. The pictures are stored in a folder and mysql is refering to it. It seems the $_GET['username'] is not coming along to the deletefile.php. Echoing it results in blank page.

 

So what I'm trying to do is, get the form to process within the admin page or pass the username variable to the deletefile.php with $_GET or $row['username']

 

Can you help me out? I've tried allot, but it's complicated for me.

 

Oh, almost forgot: The !profile_id in deletefile.php is just how the code was, but not needed, because i want to delete the pics as administrator. (it will redirect me to the index page). This was al bases on sessions, which I don't want to handle users.

 

// link in the admin page:
// | <a href='administrator.php?delpic&id=<?php echo $row['id'];?>'>Delete Pics)</a> |
<?php 
}
//action: delete pictures -----------------------------------------------------------------------------
if (isset($_GET['delpic']) && isset($_GET['id'])) {
$id = (int) $_GET['id'];
if ($id == 0) {
	die("Invalid ID provided.");
}

if($_GET['id'])
{

$sql="SELECT `id`, `first`, `last`, `username`, `email`, `about`, `level` from `users` WHERE `id`='".$_GET['id']."'";
$res=mysql_query($sql);
$row=mysql_fetch_assoc($res);

if (!isset($_POST['deletePic']))
{
?>

<!--------------------DELETE PICTURES---------------------//-->

<html>
<head>
</head>

<body>   
<div class="dividerp">

<?php

$sql2 = "SELECT `profile_id`, `title`, `size`, `type`, `reference` FROM user_photos WHERE profile_id=".$_GET['id']."";
$res2 = mysql_query($sql2) or die(mysql_error());
            
if(mysql_num_rows($res2) > 0)
	{

	echo "<strong>Delete Pictures for</strong><br/><br/>";
	echo "<form name=\"deletefile\" method=\"post\" action=\"deletefile.php\">";

	while($file = mysql_fetch_array($res2))
		{
			echo "<br/><br/><input name=\"files[]\" type=\"checkbox\" value=\"".$file['reference']."\">";
			echo "<a href=\"".$row['username']."/pics/".$file['reference']."\"/>
			<img src=\"".$row['username']."/pics/thumbs/".$file['reference']."\"/></a>";
		}
	echo "<br/><br/><input type=\"submit\" name=\"deletePic\" value=\"Delete Files\">";

	echo "</form>";

	}
	else
	{
		echo "Don't forget to add pictures<br/>";
	}
    }
else echo header("location:index.php"); 
}
?>
</div>


<?php
// deletefile.php
session_start();
include "db_connect.php";

$profile_id = $_SESSION['id'];

if(!$profile_id) 
{	
echo "<script language=\"Javascript\" type=\"text/javascript\">
alert(\"You are not logged in!\");document.location.href='index.php';
</script>";
}
else
{            
foreach($_POST['files'] as $num => $id)
{
	//delete reference in database
	@mysql_query("DELETE FROM user_photos WHERE profile_id='$profile_id' AND reference='$id'"); 
	unlink($_SESSION['username']."/pics/".$id); //delete pic in directory
	unlink($_SESSION['username']."/pics/thumbs/".$id); //delete thumbnail
}
}
?>

 

I hope you understand what i'm trying to do.

 

Kind regards,

 

Martijn

Link to comment
Share on other sites

Maybe it's more clear this way:

 

What I want to do is to add the code:

 

foreach($_POST['files'] as $num => $id)
{
	//delete reference in database
	@mysql_query("DELETE FROM user_photos WHERE profile_id='$profile_id' AND reference='$id'"); 
	unlink($_SESSION['username']."/pics/".$id); //delete pic in directory
	unlink($_SESSION['username']."/pics/thumbs/".$id); //delete thumbnail
}

 

to my admin page but not with the $_SESSION, but with $_GET or $row variable

 

But i Need to alter the foreach $_POST variable to deletePic ?

And what do I need to do with the method post action field?

Link to comment
Share on other sites

if your script can't find the path, I expect these two lines are incomplete: (this is just a guess, but makes sense, read on.)

 

unlink($_SESSION['username']."/pics/".$id); //delete pic in directory
unlink($_SESSION['username']."/pics/thumbs/".$id); //delete thumbnail

 

in the sense that an id is normally just a number, and a picture also needs to have an extension. I'm guessing they're jpeg files, so if I'm right you would need something like: (added extensions)

unlink($_SESSION['username']."/pics/".$id.".jpg"); //delete pic in directory
unlink($_SESSION['username']."/pics/thumbs/".$id.".jpg"); //delete thumbnail

Link to comment
Share on other sites

Hi,

 

No, that's not the problem. When the $_SESSION in deletefile.php is set it deletes the picture of the administrator session, because the admin has been granted the session.

What i've tried was:

 

foreach($_POST['files'] as $num => $id)
{
	//delete reference in database
	@mysql_query("DELETE FROM user_photos WHERE profile_id='$profile_id' AND reference='$id'"); 
	unlink($_GET['username']."/pics/".$id); //delete pic in directory
	unlink($_GET['username']."/pics/thumbs/".$id); //delete thumbnail
}

 

But it then say's the picture path can't be found. The add picture script makes a folder from the username and that's the one that seems to only be found if the $_SESSION is used.

That's my guess, because the path is there. The reference has the picture and extension.

 

I've also tried it with the form page to POST the usernames variable and with $_GET POST in deletefile.php but it just does'nt get where to get the username from.

 

The other thing i've tried was:

 

foreach($_POST['files'] as $num => $id)
{
	//delete reference in database
	@mysql_query("DELETE FROM user_photos WHERE profile_id='$profile_id' AND reference='$id'"); 
	unlink($row['username']."/pics/".$id); //delete pic in directory
	unlink($row['username']."/pics/thumbs/".$id); //delete thumbnail
}

 

But then again, the variable is'nt getting to the deletefile.php

 

With that i'm stuck, so i though if you could help me with the foreach to get it in (or maybe after) the form without causing a invalid argument foreach error, I would be very glad.

Any solution i'm happy with.

 

Kind regards,

 

Martijn

Link to comment
Share on other sites

I've got the error out of the foreach (see code below)

 

But now i'm having problems with pushing the delete button. It's not executing the deleting of pictures, but only get's to the Invalid ID provided in the first part of the code.

 

Do I have it in the wrong order? Is the $post action wrong?

 

Please do help me out!

 

//action: delete pictures -----------------------------------------------------------------------------
if (isset($_GET['delpic']) && isset($_GET['id'])) {
$id = (int) $_GET['id'];
if ($id == 0) {
	die("Invalid ID provided.");
}

if($_GET['id'])
{

$sql="SELECT `id`, `first`, `last`, `username`, `email`, `about`, `level` from `users` WHERE `id`='".$_GET['id']."'";
$res=mysql_query($sql);
$row=mysql_fetch_assoc($res);

if (!isset($_POST['deletePic']))
{
?>
<html>
<head>
</head>

<body>

<!--------------------DELETE PICTURES---------------------//-->
    
<div class="dividerp">

<?php

$sql2 = "SELECT `profile_id`, `title`, `size`, `type`, `reference` FROM user_photos WHERE profile_id=".$_GET['id']."";
$res2 = mysql_query($sql2) or die(mysql_error());
            
if(mysql_num_rows($res2) > 0)
	{

	echo "<strong>Delete Pictures for account: ".$row['username']." </strong><br/><br/>";
	echo "<form name=\"deletefile\" method=\"post\" action=\"administrator.php?delpic&id=".$id."\">";

	while($file = mysql_fetch_array($res2))
		{
			echo "<br/><br/><input name=\"deletePic[]\" type=\"checkbox\" value=\"".$file['reference']."\">";
			echo "<a href=\"".$row['username']."/pics/".$file['reference']."\"/>
			<img src=\"".$row['username']."/pics/thumbs/".$file['reference']."\"/></a>";
		}
	echo "<br/><br/><input type=\"submit\" name=\"deletePic\" value=\"Delete Files\">";

	echo "</form>";

if(is_array($_POST['deletePic']))
foreach($_POST['deletePic'] as $num => $id)
{
	//delete reference in database
	@mysql_query("DELETE FROM user_photos WHERE profile_id='$profile_id' AND reference='$id'"); 
	unlink($row['username']."/pics/".$id); //delete pic in directory
	unlink($row['username']."/pics/thumbs/".$id); //delete thumbnail
}	

	}
	else
	{
		echo "Vergeet niet om foto's toe te voegen<br/>";
	}
    }
else echo header("location:index.php"); 
}
?>


</div>

<?php
}

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.