Jump to content

Figuring out functions...


lordphate

Recommended Posts

Okay so i'm trying to call a function from a form...

 

Basically i'm trying to display a photo, and have a text box under it saying "edit caption here" , i wrote a edit_caption function and when they click "edit caption" button, it updates the caption....

 

this is what i have

 

[code=php:0]
<?php

include ("../inc/config.inc.php");
include ("../inc/globals.inc.php");


switch ($_GET[action]) {
case 'edit': 
edit_caption();
break;		

case 'delete':
delete_photo();
		break;

}




$result_array = array();
$counter = 0;

$cid = (int)($_GET['cid']);
$pid = (int)($_GET['pid']);



if( empty($cid) && empty($pid) )
{
	$number_of_categories_in_row = 4;

	$result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id)
					FROM gallery_category as c
					LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
					GROUP BY c.category_id" );
	while( $row = mysql_fetch_array( $result ) )
	{
		$result_array[] = "<a href='edit.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")";
	}
	mysql_free_result( $result );	

	$result_final = "<tr>\n";

	foreach($result_array as $category_link)
	{
		if($counter == $number_of_categories_in_row)
		{	
			$counter = 1;
			$result_final .= "\n</tr>\n<tr>\n";
		}
		else
		$counter++;

		$result_final .= "\t<td>".$category_link."</td>\n";
	}

	if($counter)
	{
		if($number_of_categories_in_row-$counter)
		$result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'> </td>\n";

		$result_final .= "</tr>";
	}
}



else if( $cid && empty( $pid ) )
{
	$number_of_thumbs_in_row = 5;

	$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos//// WHERE photo_category='".addslashes($cid)."'" );
	$nr = mysql_num_rows( $result );
	if( empty( $nr ) )
	{
		$result_final = "\t<tr><td>No Category found</td></tr>\n";
	}
	else
	{
		while( $row = mysql_fetch_array( $result ) )
		{
			$result_array[] = "<a href='edit.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";
		}
		mysql_free_result( $result );	

		$result_final = "<tr>\n";

		foreach($result_array as $thumbnail_link)
		{
			if($counter == $number_of_thumbs_in_row)
			{	
				$counter = 1;
				$result_final .= "\n</tr>\n<tr>\n";
			}
			else
			$counter++;

			$result_final .= "\t<td>".$thumbnail_link."</td>\n";
		}

		if($counter)
		{
			if($number_of_photos_in_row-$counter)
		$result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n";

			$result_final .= "</tr>";
		}
	}
}

else if( $pid )
{
	$result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" );
	list($photo_caption, $photo_filename) = mysql_fetch_array( $result );
	$nr = mysql_num_rows( $result );
	mysql_free_result( $result );	

	if( empty( $nr ) )
	{
		$result_final = "\t<tr><td>No Photo found</td></tr>\n";
	}
	else
	{
		$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
		list($category_name) = mysql_fetch_array( $result );
		mysql_free_result( $result );	

		$result_final .= "<tr>\n\t<td>
					<a href='edit.php'>Categories</a> > 
					<a href='edit.php?cid=$cid'>$category_name</a></td>\n</tr>\n";

		$result_final .= "<tr>\n\t<td align='center'>
				<br />
				<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />
				<br />
				$photo_caption

				<br><br><form name='option' action='edit.php' method='get'>
				<textarea name='caption' cols='30' rows='8'></textarea><br>
				<input type=button value='Edit Caption' name='edit'><br>
				<input type=button value='Delete Photo' name='delete'><br>
				</td>
				</tr>";
	}
}

?>
<html>
<head>
<title>Edit Photo</title>
</head>
<body>
<table width='100%' border='0' align='center' style='width: 100%;'>
<?php echo $result_final ?>


</table>
</body>
</html>

[/code]

Link to comment
Share on other sites

From what I can see, your switch statement is looking for the a variable called action, looking through your code there is no var passed back to the script called that.

 

In the $result_final where you build the form to submit, you have the buttons named edit and delete, but not action.

 

I would change the name of the buttons to action and then change either the case in the switch to be the same as the value in the button. Or you could change the value part of the buttons to match what you have in the switch.

 

So as an example if you keep the switch statement the same then you would change your submit buttons to the below

  <input type="submit" name="action" value="Edit">
  <input type="submit" name="action" value="Delete">

 

Try those suggestions and see how you get on.

 

Link to comment
Share on other sites

Okay it is now submitting but not changing the caption

 

edit.php?caption=sdg&action=Edit is the url given,

 

here is my globals.inc.php

 



<?php


function edit_caption( $photo_id, $new_caption, $new_category )
{
mysql_query( "UPDATE gallery_photo SET photo_caption='".addslashes( $new_caption )."', photo_category='".addslashes( $new_category )."' WHERE photo_id='".addslashes( $photo_id )."'"  );
}

//edit_caption( PHOTO_ID, “New Caption”, NEW_CATEGORY_ID );//



function delete_photo($photo_id)
{
$result = mysql_query("
   SELECT photo_filename
   FROM gallery_photo
   WHERE photo_id = '" . addslashes($photo_id) . "'
");  
list($filename) = mysql_fetch_array($result);
mysql_free_result($result);

unlink($images_dir . '/' . $filename);

mysql_query("
   DELETE FROM gallery_photo
   WHERE photo_id='" . addslashes($photo_id) . "'
");
}

//delete_photo(PHOTO_ID);//

function add_category( $category_name )
{
mysql_query( "INSERT INTO gallery_category(`category_name`) VALUES('".addslashes( $category_name )."' )" );
}

//add_category( “Category Name” );//

function edit_category( $category_id, $new_name )
{
mysql_query( "UPDATE gallery_category SET category_name='".addslashes( $new_name )."' WHERE category_id='".addslashes( $category_id )."'" );
}

//edit_category( CATEGORY_ID, “New Name” );//

function delete_category( $category_id )
{
global $images_dir;
$result = mysql_query( "SELECT photo_filename FROM gallery_photo WHERE photo_category='".addslashes( $category_id )."'" );  
while( $row = @mysql_fetch_array( $result ))
{
   unlink($images_dir."/".$row[0]);
}
mysql_query( "DELETE FROM gallery_photo WHERE photo_category='".addslashes( $category_id )."'" );
mysql_query( "DELETE FROM gallery_category WHERE category_id='".addslashes( $category_id )."'" );
}

//delete_category( CATEGORY_ID );//

?>

Link to comment
Share on other sites

In the code you listed below your edit_caption function is not receiving any parameters, but your include file lists parameters to that function. Try passing across the relevant fields from the form to the function, if you have not already seen and corrected that.

 

Okay so i'm trying to call a function from a form...

 

Basically i'm trying to display a photo, and have a text box under it saying "edit caption here" , i wrote a edit_caption function and when they click "edit caption" button, it updates the caption....

 

this is what i have

 

[code=php:0]
<?php

include ("../inc/config.inc.php");
include ("../inc/globals.inc.php");


switch ($_GET[action]) {
case 'edit': 
edit_caption();
break;		

case 'delete':
delete_photo();
		break;

}




$result_array = array();
$counter = 0;

$cid = (int)($_GET['cid']);
$pid = (int)($_GET['pid']);



if( empty($cid) && empty($pid) )
{
	$number_of_categories_in_row = 4;

	$result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id)
					FROM gallery_category as c
					LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
					GROUP BY c.category_id" );
	while( $row = mysql_fetch_array( $result ) )
	{
		$result_array[] = "<a href='edit.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")";
	}
	mysql_free_result( $result );	

	$result_final = "<tr>\n";

	foreach($result_array as $category_link)
	{
		if($counter == $number_of_categories_in_row)
		{	
			$counter = 1;
			$result_final .= "\n</tr>\n<tr>\n";
		}
		else
		$counter++;

		$result_final .= "\t<td>".$category_link."</td>\n";
	}

	if($counter)
	{
		if($number_of_categories_in_row-$counter)
		$result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'> </td>\n";

		$result_final .= "</tr>";
	}
}



else if( $cid && empty( $pid ) )
{
	$number_of_thumbs_in_row = 5;

	$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos//// WHERE photo_category='".addslashes($cid)."'" );
	$nr = mysql_num_rows( $result );
	if( empty( $nr ) )
	{
		$result_final = "\t<tr><td>No Category found</td></tr>\n";
	}
	else
	{
		while( $row = mysql_fetch_array( $result ) )
		{
			$result_array[] = "<a href='edit.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";
		}
		mysql_free_result( $result );	

		$result_final = "<tr>\n";

		foreach($result_array as $thumbnail_link)
		{
			if($counter == $number_of_thumbs_in_row)
			{	
				$counter = 1;
				$result_final .= "\n</tr>\n<tr>\n";
			}
			else
			$counter++;

			$result_final .= "\t<td>".$thumbnail_link."</td>\n";
		}

		if($counter)
		{
			if($number_of_photos_in_row-$counter)
		$result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n";

			$result_final .= "</tr>";
		}
	}
}

else if( $pid )
{
	$result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" );
	list($photo_caption, $photo_filename) = mysql_fetch_array( $result );
	$nr = mysql_num_rows( $result );
	mysql_free_result( $result );	

	if( empty( $nr ) )
	{
		$result_final = "\t<tr><td>No Photo found</td></tr>\n";
	}
	else
	{
		$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
		list($category_name) = mysql_fetch_array( $result );
		mysql_free_result( $result );	

		$result_final .= "<tr>\n\t<td>
					<a href='edit.php'>Categories</a> > 
					<a href='edit.php?cid=$cid'>$category_name</a></td>\n</tr>\n";

		$result_final .= "<tr>\n\t<td align='center'>
				<br />
				<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />
				<br />
				$photo_caption

				<br><br><form name='option' action='edit.php' method='get'>
				<textarea name='caption' cols='30' rows='8'></textarea><br>
				<input type=button value='Edit Caption' name='edit'><br>
				<input type=button value='Delete Photo' name='delete'><br>
				</td>
				</tr>";
	}
}

?>
<html>
<head>
<title>Edit Photo</title>
</head>
<body>
<table width='100%' border='0' align='center' style='width: 100%;'>
<?php echo $result_final ?>


</table>
</body>
</html>

[/code]

Link to comment
Share on other sites

case 'Edit':

edit_caption($pid, $caption, $cid);

break;

 

 

$cid = (int)($_GET['cid']);

$pid = (int)($_GET['pid']);

    $caption= ($_GET['caption']);

 

function edit_caption( $pid, $caption, $cid )

{

mysql_query( "UPDATE gallery_photo SET photo_caption='".addslashes( $caption )."', photo_category='".addslashes( $cid )."' WHERE photo_id='".addslashes( $pid )."'"  );

}

 

Still not working correctly, it is going back to the main page like it WOULD work

 

 

 

Link to comment
Share on other sites

Try putting a die() statement within the function to see why its failing.

 

mysql_query( "UPDATE gallery_photo SET photo_caption='".addslashes( $caption )."', photo_category='".addslashes( $cid )."' WHERE photo_id='".addslashes( $pid )."'"  ) or die(mysql_error());

 

Also... whats with all the unneeded brackets?

 

$cid = (int) $_GET['cid'];
$pid = (int) $_GET['pid'];
$caption = $_GET['caption'];

Link to comment
Share on other sites

I am getting 0 errors when i do insert the die statement...

 

I think it's because it's not passing along the cid and pid statements..

 

how do i make cid and pid still in the URL for the $_GET command?

 

Here is the regular URL to view the photo:

edit.php?cid=1&pid=9

 

here's what i get when i hit the edit button:

edit.php?caption=fSAF&action=Edit

 

Shouldn't i have:

edit.php?cid=1&pic=9&caption=fSAF&action=Edit  ???

Link to comment
Share on other sites

In your form you could try adding 2 hidden input fields called cid and pid, that way when the form is sent you can then retrieve them as your other vars from the form.

 

You would need to populate the value columns of the hidden field with the correct values, add the below before the buttons and especially before the end form tag

 


  <input type='hidden' name='cid' value='$cid'><input type='hidden' name='pid' value='$pid'>

 

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.