0

I have the follow code.

$acct="SELECT acctype FROM users WHERE username='$username'";

Then I have this If statement

if ($acct="customer") {
Header("Location: user/dashboard.php");
}
else {
Header("Location: other/dashboard.php");
}

Sample database output

mysql> select * from users;
+----+----------+----------------------------------+----------+----------+----------+
| id | username | password                         | fullname | location | acctype  |
+----+----------+----------------------------------+----------+----------+----------+
| 14 | customer | 91ec1f9324753048c0096d036a694f86 | customer | customer | customer |
| 15 | mfg      | fcc66ac1c0e07a00b56b0dc4c0902567 | mfg      | mfg      | mfg      |
| 16 | designer | 230ace927da4bb74817fa22adc663e0a | designer | designer | designer |
| 19 | both     | f6cb3e816496528d4187db53bc66567f | both     | both     | both     |
+----+----------+----------------------------------+----------+----------+----------+

So my goal is to have user when they login if their acctype is customer go to user/dashboard.php

If the acctype is anthing else go to other/dashboard.php

For some reason it is ignoring the acctype and just going to the first if condition.

Running the Query through MySQL will output the correct response.

mysql> SELECT acctype FROM users WHERE username='customer';
+----------+
| acctype  |
+----------+
| customer |
+----------+
1 row in set (0.00 sec)

mysql> SELECT acctype FROM users WHERE username='both';
+--------+
| acctype|
+--------+
| both   |
+--------+
1 row in set (0.00 sec)

Where am i going wrong?

Brian Curless
  • 235
  • 4
  • 13
  • 1
    You're assigning using `=` instead of comparing `==` in your `if ($acct="customer")` - Do `if($acct=="customer")` – Funk Forty Niner Jun 25 '14 at 18:54
  • 1
    `"SELECT acctype FROM users WHERE username='$username'"` does not equal `"customer"`. You are actually *running* that SQL query, right? – gen_Eric Jun 25 '14 at 18:55
  • 1
    You'll need to query the database and fetch the resulting data. Setting `$acct` to your query string will not query the database. – showdev Jun 25 '14 at 18:55
  • **Steps:** 1) Query DB 2) Use a (while) loop and fetch 3) If it exists (do something) --- You'll find your answer in [`this Q&A`](http://stackoverflow.com/q/16377823/) (and piece it all together), where OP is fetching from DB, and has also made the similar mistake. – Funk Forty Niner Jun 25 '14 at 19:14

2 Answers2

0

This should work:

    $query = "SELECT acctype FROM users WHERE username='$username'";
    if($resultSet = $mysqli->query($query)){
      while($col = $resultSet->fetch_field()){
          if($col->acctype == 'customer'){
          Header("Location: user/dashboard.php");
        }else{
           Header("Location: other/dashboard.php");
        }
       }
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
mtbjay
  • 76
  • 4
0

So i agree with Rocket your php coding need some work.

First start out with the request. You call the by using mysqli_query and then setting "results" equal to it.

$results = mysqli_query("SELECT acctype FROM users WHERE username='$username'");

Then create a variable to read each row.

$row = mysql_fetch_row($result1);

WE only want the first result so ...

if ($row[0]=="customer") {
Header("Location: user/dashboard.php");
}
else {
Header("Location: other/dashboard.php");
}

"$row[0]" will read the first line in the database.

That should work.

Anthony Fornito
  • 425
  • 1
  • 7
  • 19