Naveed786 Posted July 25, 2020 Share Posted July 25, 2020 (edited) 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 July 25, 2020 by Naveed786 Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/ Share on other sites More sharing options...
requinix Posted July 25, 2020 Share Posted July 25, 2020 Make sure the code in your editor is correctly indented. Pay attention to where your {s and }s are. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580015 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 (edited) 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 July 25, 2020 by Naveed786 Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580018 Share on other sites More sharing options...
requinix Posted July 25, 2020 Share Posted July 25, 2020 It took me about 10 seconds to see it. I'm not going to tell you where the problem is. I'm trying to teach you how to find the problem on your own. What editor or IDE are you using to write code? Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580019 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 I am using Notepad ++, actually this is my first project in PHP, normally i am developing desktop apps sorry just beginner in PHP. Any help will be great. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580020 Share on other sites More sharing options...
requinix Posted July 25, 2020 Share Posted July 25, 2020 Can you post a screenshot of what your editor looks like with the code in it? Your post has all the code on one line and I can only assume it looks better in the editor. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580021 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 Yes sure, here i am attaching screen shot. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580022 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 Error is on line 11 now Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580023 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 16 minutes ago, Naveed786 said: Yes sure, here i am attaching screen shot. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580024 Share on other sites More sharing options...
requinix Posted July 25, 2020 Share Posted July 25, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580025 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 15 minutes ago, requinix said: 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. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580027 Share on other sites More sharing options...
requinix Posted July 25, 2020 Share Posted July 25, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580028 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580030 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580035 Share on other sites More sharing options...
Barand Posted July 25, 2020 Share Posted July 25, 2020 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 ..."); 1 Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580036 Share on other sites More sharing options...
Naveed786 Posted July 25, 2020 Author Share Posted July 25, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311214-syntax-error-need-help/#findComment-1580037 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.