Jump to content

PHP If Else Error when Submiting Upload


jassikundi

Recommended Posts

Hi everyone, I'm trying to do this IF Else statement for a file upload using php to my server. I keep getting an error on after the else part can someone check this code for me? The error is on line 66 for this that would be the "if($session->isAdmin()){" after the else. I'm thinking that I cant use another "if" after an else? Hence why everything before the "if" statement is displayed then an error after that? If I remove that "if" statement then my link for a admin user will display for everyone. I'm new to php so any help would be great. Thanks.

 

[pre]//Writes the measurments to the server

if(move_uploaded_file($_FILES['measurements']['tmp_name'], $target))

{

 

//Tells customer if send succesfull

echo "<h1>Logged In</h1>";

  echo "Welcome <b>$session->username</b>, you are logged in."

      ."[<a href=\"../userinfo.php?user=$session->username\">My Account</a>]   "

      ."[<a href=\"../useredit.php\">Edit Account</a>]   "

  ."[<a href=\"../quote/quote.php\">Quote</a>]   ";

    if($session->isAdmin()){

      echo "[<a href=\"../admin/admin.php\">Admin Center</a>]   ";

  }

  echo "[<a href=\"../process.php\">Logout</a>]";

echo "Thankyou, your quote request has been sent directly to our sales team, you will receive a reply shortly.";

 

}

else{

?>

<?

//Gives an error if send unsuccessful

echo "<h1>Logged In</h1>";

  echo "Welcome <b>$session->username</b>, you are logged in."

      ."[<a href=\"../userinfo.php?user=$session->username\">My Account</a>]   "

      ."[<a href=\"../useredit.php\">Edit Account</a>]   "

  ."[<a href=\"../quote/quote.php\">Quote</a>]   ";

      if($session->isAdmin()){

      echo "[<a href=\"../admin/admin.php\">Admin Center</a>]   ";

  }

  echo "[<a href=\"../process.php\">Logout</a>]";

echo "Sorry, there was a problem submitting your quote";

?>

<?

}

?>[/pre]

Link to comment
Share on other sites

$session is being taken from an include file called session.php this file contains all the user session data. The session.php file is very large and would not want to post the entire code. So I've taken the main part out:

 

[pre]  /**

    * isAdmin - Returns true if currently logged in user is

    * an administrator, false otherwise.

    */

  function isAdmin(){

      return ($this->userlevel == ADMIN_LEVEL ||

              $this->username  == ADMIN_NAME);

  }

 

/**

* Initialize session object Initialized before

* the form object because the form uses session variables,

* which cannot be accessed unless the session has started.

*/

$session = new Session;[/pre]

 

Ok I've noticed that it is in fact the if($session->isAdmin()){ line causing the problem its not the if or else. As I removed them from both parts and allowed the form result to display everything besides the admin link. Now I'm not quite sure what I would to check if its an Admin viewing the page as this if($session->isAdmin()){ has worked fine for me in other php pages e.g. the actual quote form.

Link to comment
Share on other sites

Fatal error: Call to a member function on a non-object in /homepages/5/d222347135/htdocs/quote/sendquote.php on line 66

 

is the error I get :S

 

The error you describe means that $session is not an object, which means it's not be created properly. Make sure you are using a require() and not include() when including this session.php file.

 

Couple of things to help debug the issue:

 

1) After the "$session = new Session;" put:

print "Session classname: ".get_class($session);

And see if it prints "Session classname: Session"

 

2) Make sure this session.php file is getting included by printing out the list of included files. You can do that with this function:

print_r(get_included_files());

Link to comment
Share on other sites

OK I managed to work it all out it was simply the use of incorrect } they was not balancing each other out. Thanks for the help everyone. However now I'm stuck on the if statement, having decided that when the user submits no file (file upload) the form used to simple skip to the error:

[pre]}

else {

 

//Gives an error if send unsuccessful

echo "Sorry, there was a problem submitting your quote";

}

?>[/pre]

But since I've added the following php the form assumes that the file has been uploaded and continues to give the message that it was successful, I'm guessing incorrect use of if statements? As its displaying the links regardless of the form being submitted correctly or incorrectly:

 

[pre]//Writes the measurments to the server

if(move_uploaded_file($_FILES['measurements']['tmp_name'], $target)){} 

if($session->logged_in){

echo "<h1>Logged In</h1>";

  echo "Welcome <b>$session->username</b>, you are logged in."

      ."[<a href=\"../userinfo.php?user=$session->username\">My Account</a>]   "

      ."[<a href=\"../useredit.php\">Edit Account</a>]   "

  ."[<a href=\"../quote/quote.php\">Quote</a>]   ";}

      if($session->isAdmin()){

      echo "[<a href=\"../admin/admin.php\">Admin Center</a>]   ";

 

//Tells customer if send succesfull

echo "Thankyou, your quote request has been sent directly to our sales team, you will receive a reply shortly.";[/pre]

 

Sorry to be a pain, but I have tried  ???

Link to comment
Share on other sites

You should work on indenting your code properly so your braces line up. I think it will help find your errors. Here is your code, unchanged, just indented:

 

//Writes the measurments to the server
if(move_uploaded_file($_FILES['measurements']['tmp_name'], $target))
{
}
if($session->logged_in)
{
  echo "<h1>Logged In</h1>";
  echo "Welcome $session->username, you are logged in."
        ."[<a href=\"../userinfo.php?user=$session->username\">My Account[/url]]   "
        ."[<a href=\"../useredit.php\">Edit Account[/url]]   "
        ."[<a href=\"../quote/quote.php\">Quote[/url]]   ";
}
if($session->isAdmin())
{
  echo "[<a href=\"../admin/admin.php\">Admin Center[/url]]   ";
  //Tells customer if send succesfull 
  echo "Thankyou, your quote request has been sent directly to our sales team, you will receive a reply shortly.";

 

You can see the first IF statement doesn't execute any code if the file is moved. And the last IF doesn't have a close brace.

 

Hope that helps

Link to comment
Share on other sites

Hi thanks for the heads up will do that from now, also the last IF did have a close brace just didn't past the last line oppz. The first IF statement is using this $target.

 

[pre]//This is the directory where images will be saved

$target = "images/";

$target = $target . basename( $_FILES['measurements']['name']);[/pre]

Link to comment
Share on other sites

SOLVED

 

I don't believe that I was supposed to enter the remaining IF statements within the {}. Here is the code used now:

 

[pre]if (move_uploaded_file($_FILES['measurements']['tmp_name'], $target))

{

  if($session->logged_in)

  {

echo "<h1>Logged In</h1>";

  echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"

      ."[<a href=\"../userinfo.php?user=$session->username\">My Account</a>]   "

      ."[<a href=\"../useredit.php\">Edit Account</a>]   "

  ."[<a href=\"../quote/quote.php\">Quote</a>]   ";}

      if($session->isAdmin()){

      echo "[<a href=\"../admin/admin.php\">Admin Center</a>]   ";

 

//Tells customer if send succesfull

echo "Thankyou, your quote request has been sent directly to our sales team, you will receive a reply shortly.";

}

 

 

else {

 

//Gives an error if send unsuccessful

echo "Sorry, there was a problem submitting your quote";

}[/pre]

 

THANKS TO EVERYONE THAT HELPED.  ;D

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.