Jump to content

Syntax Error Need Help


Naveed786

Recommended Posts

Hi All,

I havefollowing code and getting error "Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in C:\xampp\htdocs\SMR\index.php on line 13". Wehen i remove elseif and keep only if it just checks the first condition and ignor the second. When i put back elseif i get error. Please help to fix this issue.

 

 

<?php session_start(); error_reporting(0); include('includes/dbconnection.php'); if(isset($_POST['login'])) { $adminuser=$_POST['username']; $password=md5($_POST['password']); $userlvl=$_POST['userlvl']; $query=mysqli_query($con,"select ID from tbladmin where UserName='$adminuser' && Password='$password' && userlvl='$userlvl' "); $ret=mysqli_fetch_array($query); elseif($ret>0 || userlvl==1){ $_SESSION['cvmsaid']=$ret['ID']; header('location:dashboard.php'); } elseif($ret>0 || userlvl==2){ $_SESSION['cvmsaid']=$ret['ID']; header('location:dashboard-supp.php'); } else{ $msg="Invalid Details."; } } ?>

 

 

 

Please help to fix this issue.

Kind Regards,

Naveed.

Edited by Naveed786
Link to comment
Share on other sites

Hi Requinix,

Thanks for your reply, actually i tried a lot not able to figure out where exactly it has issue, if you can have a look and point out what needs to be amended to make it working.

 

I will be grateful.

Once again thanks for help! 

Edited by Naveed786
Link to comment
Share on other sites

image.png

See those red arrows? They point out the different pieces of your if statement. See how they aren't all indented the same way? Fix the indentation so that they all line up. That makes it much easier to see that they're all related.
See the red circles? They show the {s and }s that you're using in the if. Every { used in the if statement needs to match up with a } used in another branch (else if or else) of the if statement.

Your second screenshot shows that you're counting the braces, which is good, but you're not thinking about what each "opening" and "closing" means. You see how #2 opens and closes before #3? And how #3 opens and closes before #4? 

Link to comment
Share on other sites

15 minutes ago, requinix said:

image.png

See those red arrows? They point out the different pieces of your if statement. See how they aren't all indented the same way? Fix the indentation so that they all line up. That makes it much easier to see that they're all related.
See the red circles? They show the {s and }s that you're using in the if. Every { used in the if statement needs to match up with a } used in another branch (else if or else) of the if statement.

Your second screenshot shows that you're counting the braces, which is good, but you're not thinking about what each "opening" and "closing" means. You see how #2 opens and closes before #3? And how #3 opens and closes before #4? 

Thanks i think i understood the issue, but when i closed the first if before opening the elseif error gone but now most probably it's ignoring the rest of too elseif and moving directly to else part and showing "Wrong Credentials" on login page and when i enter the username and password and user level it redirects back to login page it's not showing dashboard. Please see the attached screenshot.

 

 

 

Code.jpg

Link to comment
Share on other sites

Take a look at the logic here.

1. if the "login" button (?) was clicked,
2. Else if $ret>0 or userlvl is 1,
3. Else if $ret>0 or userlvl is 2,
4. Else

Do those all make sense together? Not really. What you want is

1. If the "login" button was clicked

and that's it. The stuff about $ret and userlvl happens inside the if. Which means you need another if.

  • Great Answer 1
Link to comment
Share on other sites

Yes you are right now all error gone away, i am reading through a lot of articles the code is same like in screenshot below.

Now what's happening once i pass credentials the first 2  and 4th condition are getting evaluated but the 3rd getting ignored i have checked the code several times and can not find what's wrong with it. Please see below screen shot.

Code.jpg

Link to comment
Share on other sites

Hi Requinix,,

 

Thanks for your time and help  you put me in right direction after eliminating the first issue i found the second but that was easy to track and fix. I analyzed your last instructions and re wrote the code again and found the issue with the variables and removed the $ret from if statement and it went well.

 

Once again thanks a lot for help.

Link to comment
Share on other sites

1 hour ago, Naveed786 said:

and can not find what's wrong with it.

when $ret > 0 you get sent to dashboard. It will not get as far the 3rd condition. Once on elseif condition is satisfied it stops processing them.

BTW, you should always have an exit instruction after a header("location ...");

  • Thanks 1
Link to comment
Share on other sites

1 minute ago, Barand said:

when $ret > 0 you get sent to dashboard. It will not get as far the 3rd condition. Once on elseif condition is satisfied it stops processing them.

BTW, you should always have an exit instruction after a header("location ...");

@Barand yes you are absolutely right i agree with you, i tried edit as well but didn't worked then i removed the $ret from the condition and executed the code it worked well. But the weakness in code you found is correct.

 

Thanks to you as well.

 

It's a great helping forum for all level programmers.

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.