проблема с другим уровнем входа пользователя - PullRequest
0 голосов
/ 21 октября 2018

У меня была проблема с многоуровневым входом пользователя в PHP MySQL.Я могу войти с помощью администратора, но ничего не происходит, когда я пытаюсь войти с другим пользователем.Есть ли что-то, что мне здесь не хватает?

Вот мой фрагмент кода

    <form action="" method="post">
        <div class="form-group">
            <label for="inputUsername">Username</label>
            <input type="text" class="form-control" name="username" placeholder="username" required>
        </div>
        <div class="form-group">
            <label for="inputPassword">Password</label>
            <input type="password" class="form-control" name="password" placeholder="Password" required>
        </div> 
        <div class="user_type">
        <table>
          <tr> 
          <td>User Type &nbsp;</td>   
          <td><select name="usertype" id="type">
          <option value="-1"> Select user type</option>
          <option value="customer">Customer</option>
          <option value="landlord">Landlord</option>
          <option value="admin">Admin</option>
          </select></td>
        <button type="submit" name="submit" class="btn btn-primary">Login</button>
   

<?php
include('connection.php');

if(isset($_POST['submit'])){

  $username=$_POST['username'];
  $password=$_POST['password'];
  $type=$_POST['usertype'];
  
  $query="SELECT * FROM `user_level` WHERE username='$username' and password='$password' 
  and type='$type'";
  $result=mysqli_query($conn,$query);
  
  while($row=mysqli_fetch_array($result)){
    if($row['username']==$username && $row['password'==$password] && 
    $row['type']=='Admin') {
    header("Location: admin_mainpage.php");
    } elseif($row['username']==$username && $row['password'==$password] && 
    $row['type']=='Land Owner'){
      header("Location: LO_mainpage.php");
    }elseif($row['username']==$username && $row['password'==$password] && 
    $row['type']=='Customer'){
      header("Location: Customer_mainpage.php");
    }
  } 
  }
?>

1 Ответ

0 голосов
/ 21 октября 2018

Вам просто нужно написать точный тип пользователя, попробуйте этот код:

<?php

if(isset($_POST['submit'])) {

  // Make sure to change "username" "password" "database" 
  $conn = mysqli_connect("localhost", "username", "password", "database"); 

  $username = $_POST['username'];
  $password = $_POST['password'];
  $type = $_POST['usertype'];

  $query="SELECT * FROM user_level WHERE username='$username' and password='$password' and type='$type'";
  $result=mysqli_query($conn,$query);

  // Just use the if statement, it will test if a user exist with these infromation or not   
  if( $row = mysqli_fetch_array($result) ) {

    // Now we are sure that we entered a right information and there a user,  you just need to test the type using the switch statement
    switch ($type) {
        case "admin" :  
            header("Location: admin_mainpage.php");break;
        case "landlord" :
            header("Location: LO_mainpage.php");break;
        case "customer" :
            header("Location: Customer_mainpage.php");break;
    }
  }

}

?>

<form  method="post">
    <div class="form-group">
       <label for="inputUsername">Username</label>
       <input type="text" class="form-control" name="username" placeholder="username" required>
    </div>
    <div class="form-group">
       <label for="inputPassword">Password</label>
       <input type="password" class="form-control" name="password" placeholder="Password" required>
    </div>
    <div class="user_type">
    <table>
        <tr>
           <td>User Type &nbsp;</td>
           <td>
              <select name="usertype" id="type" required>
                 <option value="" selected> Select user type</option>
                 <option value="customer" >Customer</option>
                 <option value="landlord" >Landlord</option>    
                 <option value="admin" >Admin</option>  
              </select>
           </td>
           <td>
           <button type="submit" name="submit" class="btn btn-primary">Login</button>
           </td>
        </tr>
    </table>
</form>    

Это таблица sql:

DROP TABLE IF EXISTS `user_level`;
CREATE TABLE `user_level` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `type` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
)

INSERT INTO `user_level` (`id`, `username`, `password`, `type`) VALUES
(1, 'mac',  '123',  'admin'),
(2, 'krisha',   '123',  'customer'),
(3, 'Haris',    '123',  'landlord');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...