[SOLVED] Call to undefined function

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
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

[SOLVED] Call to undefined function

Post by Jim_Bo »

Hi,

with the following code .. It all works fine as it is ..

Code: Select all

<?php

    include("config.inc.php");

    if( empty($_POST['action']) )
    {

    }
    else if( strcasecmp($_POST['action'], "Find Photo")==0 )
    {
    
	$result = mysql_query( "SELECT photo_caption,photo_filename,photo_category FROM gallery_photos WHERE photo_id='".addslashes($_POST['photoid'])."'" );
    $nr = mysql_num_rows( $result );

    if( $nr < 1 )
    {
        echo("<font class=\"txt\">Photo not found in DB</font><br>");
        echo("<br><font class=\"txt\"><a href=\"../index.php?pages=adminindex\">Back to Photogallery Admin Page</a></font><br><br>");
        return;
    }

    $row = mysql_fetch_array( $result );
    mysql_free_result( $result );
    echo("<font class=\"txt\">Picture ID: ".$_POST['photoid']."</text><br><br>");
    echo("<a href='index.php?cid=".$row['photo_category']."&pid=".$_POST['photoid']."'><img src='".$images_dir."/tb_".$row[1]."' border='1' alt='".$row[0]."' /></a>");

    $result = mysql_query( "SELECT category_id,category_name FROM gallery_category" );
    while( $row2 = mysql_fetch_array( $result ) )
    {

        if( $row2["category_id"] == $row["photo_category"] )
        {

$category_list .=<<<__HTML_END
    <option value="$row2[0]" selected>$row2[1]</option>\n
__HTML_END;
        }
        else
        {
$category_list .=<<<__HTML_END
    <option value="$row2[0]">$row2[1]</option>\n
__HTML_END;
        }
    }
    
	mysql_free_result( $result );
    $category_list = '<select name="categoryid">'.$category_list.'</select>';
?>
 
<form name="photo_move" action="../index.php?pages=edit_photo" method="post">
  <div align="center"><b><font class="txt">Move Photo / Update Caption:</font></b><br>
    <font class="txt"><br>
    Select New Category:<br>
    <br>
    <?php echo($category_list); ?></font><br>
    <br>
    <font class="txt">Update Caption:<br>
    </font><br>
    <textarea name="caption" cols="45" rows="1"><?php echo($row["photo_caption"]); ?></textarea>
    <br>
    <br>
    <input type="hidden" value="<?php echo($_POST['photoid']); ?>" name="photoid" />
    <input type="submit" value="Submit" name="action" class='submit-button'>
  </div>
</form>
<div align="center"><br>
</div>
<form name="photo_delete" action="../index.php?pages=edit_photo" method="post">
  <div align="center"><b><font class="txt">Delete Photo:</font></b><br>
    <br>
    <input type="hidden" value="<?php echo($_POST['photoid']); ?>" name="photoid" />
    <input type="submit" value="Delete This Photo?" name="action" class="submit-button" onclick="return confirm('Are you sure you want to do delete this photo?')" />
  </div>
</form>
<center><font class="txt"><a href="../index.php?pages=adminindex">Back to Photogallery Admin Page</a></font></center><br><br>

<?php
    }
    else
    {
        if( strcasecmp($_POST['action'], "Submit")==0 && !empty( $_POST['categoryid'] ) )
        {
            edit_photo($_POST['photoid'], $_POST['caption'], $_POST['categoryid']);
        }
        else if( strcasecmp($_POST['action'], "Delete This Photo?")==0 && !empty( $_POST['photoid'] ) )
        {
            delete_photo($_POST['photoid']);
        }
        else
        {
            echo("Action not understood"); return;
        }

        echo("<font class=\"txt\">Process completed</font><br>");
        echo("<br><font class=\"txt\"><a href='../index.php?pages=adminindex'>Back to Administration Page</a></font><br><br>");
    }

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

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

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

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


?>
But soon as I add the following code around it:

Code: Select all

if(($_SESSION['user_level'] == 2) || ($_SESSION['user_level'] == 3)) {

// Above code here

} else {
	
	echo "<font class=\"txt\">You are either not logged in, or dont have permission to be here!</font><br><br>";
	echo "<font class=\"txt\"><a href=\"../index.php?pages=login_form\">Login Here</a></font><br><br>";

}
I get an error like so:

Code: Select all

Fatal error: Call to undefined function: edit_photo() in /home/noname/public_html/album/edit_photo.php on line 87
I have no idea why .. How can I fix this?

Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, obviously, you're calling edit_photo() when it hasn't been declared yet. Take it out of the conditional and test again.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

It has been declaired from the form that "post" to the code above .. which the form also has the user_level security around the code ..

Like I said .. If I take away the user_level security code .. it all works fine!

Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Okay then... there's a line number, so can you post the entire code that throws the error? Line numbers aren't useful unless we have the whole script.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Maybe my query is unclear ..

If I remove:

Code: Select all

if(($_SESSION['user_level'] == 2) || ($_SESSION['user_level'] == 3)) { // Above code here } else {        echo "<font class=\"txt\">You are either not logged in, or dont have permission to be here!</font><br><br>";    echo "<font class=\"txt\"><a href=\"../index.php?pages=login_form\">Login Here</a></font><br><br>"; }
The code works fine .. Its only when I place the above around the original code I posted .. do I start to get the following error:

Code: Select all

Fatal error: Call to undefined function: edit_photo() in /home/noname/public_html/album/edit_photo.php on line 87
that line where the error is:

Code: Select all

edit_photo($_POST['photoid'], $_POST['caption'], $_POST['categoryid']);
With the security code wraped around the code I posted .. I get similar errors with any of the functions I try to exicute ..

Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm... I'm not sure, but I think that functions that are put in conditionals aren't automatically parsed: whereas without the conditional they parse and then can be used anywhere in the file. thus:

Code: Select all

<?php
echo superfunk();

function superfunk() {
  return "Boom!";
}
?>
Works but:

Code: Select all

<?php
if(!isset($_GET['funky'])) {
  echo superfunk();
  function superfunk() {
    return "Boom!";
  }
} else {
  echo "WHA? You're not funky!";
}
?>
Doesn't work.

I tested it and yup, it acts just like I suspected.

Solution?

Declare your functions before you do other stuff.
Last edited by Ambush Commander on Sun Mar 13, 2005 8:10 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Move the function definitions before your code. The function definitions are parsed after the code is being run.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

What's tricky about it is that if the function definition ISN'T in a conditional, PHP will look ahead and parse it, so that it does seem to work. But you shouldn't rely on that.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Well im lost .. I really dont understand what use are saying ..

I also dont understand by just puting session validation around the script fails ..? :oops:

and it works fine without the user_level validation ..

Another quick question .. I have data stored in the database as:

bla bla

bla bla

when I call it to a table using

$stuff = $row['stuff'];

it displays as

bla bla bla bla ..

how can I format it to show as

bla bla

Bla bla


Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's a quirk with PHP. Functions, most of the time, can be declared anywhere in the script, as long as there not in some sort of conditional. If they are in a conditional, then they Must be defined before they're called. The fix is simple, really. Just remove the functions and replace them before the conditonal.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's not the session validation that's making it fail, it's the fact that conditionally creating functions forces PHP to parse them as they appear in the file, no read-ahead is performed.

Moved the function definitions to immediately after you enter the if, it should work again.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Thanks have it working now ..
Another quick question .. I have data stored in the database as:

bla bla

bla bla

when I call it to a table using

$stuff = $row['stuff'];

it displays as

bla bla bla bla ..

how can I format it to show as

bla bla

Bla bla
Any answers that, of should I be starting a new post .. ?


Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, the answer is simple, although it probably should be in another topic. If you take a look at the source, you'll see that it is indeed:

Code: Select all

Blah blah

Blah blah
But of course, HTML linespaces don't mean anything! You need <br />! Try nl2br() on your output.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nl2br()
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Thanks guys .. Both issues are sorted ..

Can't thank the people that help on this forum enough! ..


Great job guys ..
Post Reply