jassikundi Posted February 11, 2008 Share Posted February 11, 2008 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] Quote Link to comment Share on other sites More sharing options...
revraz Posted February 11, 2008 Share Posted February 11, 2008 Sure you can nest a IF inside an ELSE. What is the actual error? Quote Link to comment Share on other sites More sharing options...
jassikundi Posted February 11, 2008 Author Share Posted February 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 11, 2008 Share Posted February 11, 2008 where is $session getting set? can you paste the code for that? Quote Link to comment Share on other sites More sharing options...
revraz Posted February 11, 2008 Share Posted February 11, 2008 So safe to assume that $session is in fact set as a OBJECT array? Quote Link to comment Share on other sites More sharing options...
jassikundi Posted February 11, 2008 Author Share Posted February 11, 2008 $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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 11, 2008 Share Posted February 11, 2008 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()); Quote Link to comment Share on other sites More sharing options...
jassikundi Posted February 12, 2008 Author Share Posted February 12, 2008 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 ??? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 12, 2008 Share Posted February 12, 2008 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 Quote Link to comment Share on other sites More sharing options...
jassikundi Posted February 12, 2008 Author Share Posted February 12, 2008 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] Quote Link to comment Share on other sites More sharing options...
jassikundi Posted February 12, 2008 Author Share Posted February 12, 2008 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 12, 2008 Share Posted February 12, 2008 Looks good. Are you sure the Thank you shouldn't be one line down? It seems like it should be outside the IF LOGGED IN block, but still inside the IF MOVE SUCCEEDS part. Quote Link to comment Share on other sites More sharing options...
jassikundi Posted February 12, 2008 Author Share Posted February 12, 2008 Yup i've just had a look at that will move it outside the IF statement and see if it runs ok. Thanks just have to indent the page. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.