1

I'm working on a project where a user will login and it only shows records if the rep column is their name. I know there's a ton of ways to do this but the easiest way for me is to use If statements with sessions

if($_SESSION['account_type'] == 'Rep') {

    $sql = "SELECT * FROM `employee` WHERE rep='".$_SESSION['name']."' ";

} elseif ($_SESSION['account_type'] == 'Manager')  {

    $sql = "SELECT * FROM `employee` WHERE Manager='".$_SESSION['name']."' ";

} elseif ($_SESSION['account_type'] == 'Regional')  {

    $sql = "SELECT * FROM `employee` WHERE Region='".$_SESSION['regional']."' ";

} elseif ($_SESSION['account_type'] == 'Other')  {

    $sql = "SELECT * FROM `employee` ";

} else {
    echo 'no records found';
}

If your account type is a rep, show just records with your name. If your account type is manager, show records where the manager record has your name, etc etc.

For some reason, I'm not getting any records back at all; its blank.

If I change it to just:

$sql = "SELECT * FROM `employee` WHERE rep='".$_SESSION['name']."' ";

I get records back where it has the users name in the rep column; which is great but i need the if statements to work.

Any ideas?

UPDATE

Syntax error - thanks!

hamed
  • 91
  • 5
JD6969
  • 57
  • 6

1 Answers1

0

Use === or strcmp function. You shouldn't use == for String comparison.

Try if($_SESSION['account_type'] === 'Rep') and so on...

hamed
  • 91
  • 5
  • it should be in comment area and if any if condition not meet at least it should echo the else part . but he telling that he getting blank page . so somewhere in his code is error – JYoThI Apr 11 '17 at 05:39
  • you're right, i found there error (Home Simpson DOH!) – JD6969 Apr 11 '17 at 05:44
  • I don know why you said this but please visit https://www.tutorialspoint.com/mysql/mysql-where-clause.htm mysql where clause has operator like == or === it only allow = for equal condition – M. K Hossain Apr 11 '17 at 05:44
  • @hamed Do you have an explanation or reference for why you shouldn't use `==` for String comparison? – topher Apr 11 '17 at 09:36
  • @topher sure, `==` is a loose comparison, while `===` is not, take a look at http://stackoverflow.com/a/8494892/5559741 – hamed Apr 12 '17 at 05:43