Delete... [status: solved!]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Delete... [status: solved!]

Post by Dm7 »

Ok before I added that delete and editing features... it was working fine... but whenever I click on "delete" link to delete a picture that i was viewing (only when I'm logged in)... I get this error:

Code: Select all

Error performing query!
I have absolutely no idea what I did wrong. :S Of course, the url will be view.php?pix=image.jpg?a=del or view.php?pix=image.jpg?a=edit for actions to be performed.. whenever I use a variable in there, I get query error :( but when a isn't in the url, I dont get error. *scratches her head* Also deleting an image doesn't work at all either... but I didn't get any errors except "Error performing query!" mentioned earlier. *scratches her head again*

my script is

Code: Select all

<?php
session_start();
$session = $_SESSION['auth'];
if ($session == "yes")
{
	$menu = "yes";
}
else 
{
	$menu = "no";
}
?>
<html><title>Dm7.net :: Viewing</title>
<body>
<?php
#########################################
#       Admin Gallery System            #
#             view.php                  #
#########################################
include("gallery.css");
include("links.css");
include("functions_main.inc.php");
include("fields_login.inc.php");
$table = "art";
$pix = $_REQUEST['pix'];

//is a (action variable) set?
if (isset($_REQUEST['a']))
{
	//is a = del?
	if (($_REQUEST['a'] == "del") && ($_SESSION['auth'] == "yes"))
	{
		if (isset($pix))
		{
			Connect_to_db("Vars.inc.php");
			$sql = "DELETE FROM $table WHERE name = '$pix'";
			mysql_query($sql) or die("Error performing query - could not delete!". mysql_error());
			?><font color="red"><h2><center>This picture has been deleted!</center></font></h2>
			<center><?php loginmenu(); ?></center><?php ;
			mysql_close();
			exit();
		}
		else 
		{
			echo "No picture to delete from!";
		}
	}
	//is a = edit?
	if (($_REQUEST['a'] == "edit") && ($_SESSION['auth'] == "yes"))
	{
		if (isset($pix))
		{
			echo "Editing feature isn't available yet.";
			exit();
		}
		else
		{
			echo "No picture to edit from!";
		}
	}
}
		
//checks if pix variable is set.. if not, an error msg is given.

if (!isset($pix)) {
?>
<font color="red"><h2><center>Error obtaining a requested picture!</center></h2>
</font> <?php }
// Connects to db and generates the result
else { 
	Connect_to_db("Vars.inc.php"); 
	$sql = "SELECT * FROM $table WHERE name ='$pix'";
	$result = mysql_query($sql);
	if ($row = mysql_fetch_assoc($result)) {
	printf("<div align=\"center\" class=\"title\">\"%s\"</div>\n <table border='0' align='center'><tr><td><img src='%s'></td></tr><tr><td class=\"titletb\"><span class='pink'><b>Title:</b></span> %s</td></tr><tr><td class=\"titletb\"><span class='pink'><b>Date:</b></span> %s</td></tr><tr><td class=\"titletb\"><span class='pink'><b>Description/Comment:</b></span> %s</td></tr></table>",$row['title'],$row['image'],$row['title'],$row['date'],$row['comment']);
	}
	else { echo "<font color=\"red\"><h2><center>Error performing query!<br>" . mysql_error() . "</center></h2></font>"; } 
	mysql_close();
}
?><br>
<div align="center" class="titletb"><?php
loginmenu();
function loginmenu() 
{
global $menu, $menuloginview, $pix;
if($menu == "yes")
{
$edit = "<a href='view.php?pix=". $pix ."?a=edit'>Edit</a> | ";
$del = "<a href='view.php?pix=". $pix ."?a=del'>Delete</a> | ";
echo $edit ." ". $del;
	foreach($menuloginview as $key => $value)
	{
		if ($key == "logout") 
		{
			echo $value;
		}
		else 
		{
			echo $value ." | ";
		}
	}
}
}
?></div>
<p align="center" class="pink"><b>After viewing, close your window to return to the gallery page.</b></p>

</body>
</html>
Last edited by Dm7 on Mon Oct 10, 2005 4:43 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echo out $sql to see if it's correct and you may want to look at mysql_affected_rows()
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post by Dm7 »

Ok I echoed $sql... still got performing query error that i mentioned earlier.. it didn't show the echo at all (I just added echo at where $sql with delete request is). *scratches her head again :S*

Code: Select all

Connect_to_db("Vars.inc.php");
			$sql = "DELETE FROM $table WHERE name = '$pix'";
			mysql_query($sql) or die("Error performing query - could not delete!". mysql_error());
			?><font color="red"><h2><center>This picture has been deleted!</center></font></h2>
			<center><?php loginmenu(); ?></center><?php ;
			echo $sql;
			mysql_close();
			exit();
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That's because the die() function is being called, put the echo $sql; line before mysql_query() or even change the die() to die(mysql_error() . " -- $sql");

die() = exit() with message.
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post by Dm7 »

Okay.. I still get the same error:

Code: Select all

Error performing query!
...which is way later at the bottom where it calls the picture and other related information for viewing ??? (but it works when I don't use a variable in the url???).
the code I have right now with die(); replaced is..

Code: Select all

//is a (action variable) set?
if (isset($_REQUEST['a']))
{
	//is a = del?
	if (($_REQUEST['a'] == "del") && ($_SESSION['auth'] == "yes"))
	{
		if (isset($pix))
		{
			Connect_to_db("Vars.inc.php");
			$sql = "DELETE FROM $table WHERE name = '$pix'";
			mysql_query($sql) or die("Error performing query - could not delete!". mysql_error());
			?><font color="red"><h2><center>This picture has been deleted!</center></font></h2>
			<center><?php loginmenu(); ?></center><?php ;
			echo $sql;
			mysql_close();
			die(mysql_error() . " -- $sql");
		}
		else 
		{
			echo "No picture to delete from!";
		}
	}
	//is a = edit?
	if (($_REQUEST['a'] == "edit") && ($_SESSION['auth'] == "yes"))
	{
		if (isset($pix))
		{
			echo "Editing feature isn't available yet.";
			exit();
		}
		else
		{
			echo "No picture to edit from!";
		}
	}
}
I'm starting to think that the bug is originally from

Code: Select all

Connect_to_db("Vars.inc.php"); 
	$sql = "SELECT * FROM $table WHERE name ='$pix'";
	$result = mysql_query($sql);
	if ($row = mysql_fetch_assoc($result)) {
	printf("<div align=\"center\" class=\"title\">\"%s\"</div>\n <table border='0' align='center'><tr><td><img src='%s'></td></tr><tr><td class=\"titletb\"><span class='pink'><b>Title:</b></span> %s</td></tr><tr><td class=\"titletb\"><span class='pink'><b>Date:</b></span> %s</td></tr><tr><td class=\"titletb\"><span class='pink'><b>Description/Comment:</b></span> %s</td></tr></table>",$row['title'],$row['image'],$row['title'],$row['date'],$row['comment']);
	}
	else { echo "<font color=\"red\"><h2><center>Error performing query!<br>" . mysql_error() . "</center></h2></font>"; } 
	mysql_close();
}
Please help me out. :) Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unless you've only done a partial copy of the displayed error, that doesn't appear anywhere in the code you have posted. There is a similar string in your code posted, but not exactly the same. Maybe the error is happening elsewhere?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I see two query error messages and only one matches the one you are complaining about, so try:

Code: Select all

Connect_to_db("Vars.inc.php");
    $sql = "SELECT * FROM $table WHERE name ='$pix'";

// LET'S SEE WHAT YOUR QUERY REALLY IS
echo "SQL=$sql<br/>";

    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) {
    printf("<div align=\"center\" class=\"title\">\"%s\"</div>\n <table border='0' align='center'><tr><td><img src='%s'></td></tr><tr><td class=\"titletb\"><span class='pink'><b>Title:</b></span> %s</td></tr><tr><td class=\"titletb\"><span class='pink'><b>Date:</b></span> %s</td></tr><tr><td class=\"titletb\"><span class='pink'><b>Description/Comment:</b></span> %s</td></tr></table>",$row['title'],$row['image'],$row['title'],$row['date'],$row['comment']);
    }
    else { echo "<font color=\"red\"><h2><center>Error performing query!<br>" . mysql_error() . "</center></h2></font>"; }
    mysql_close();
}
My guess is that the query is successful (so no mysql_error()) but is not returning any rows because there is no match for the where clause. You should check "numrows". There are really three conditions for a SELECT: rows returned, not matches found, error.
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post by Dm7 »

The problem was really because I did url like this,

Code: Select all

?page=view.php?a=del
so it read thoe whole thing as page's variable.

It should have been

Code: Select all

?page=view.php&a=del
Thank you very helping. :D
Post Reply