below code takes data (email,password,user type) from $_POST[] and use them to search whether a user exists on the database. If yes sends a JSON object to the client and also stores cookies on the browser.

I have two user types and have them stored in two tables in my database.

  • User_Table
  • Driver_Table

I need to change the database tables depending on the user type because I have used common SQL statement for both. For that, I have implemented selection($userType) in credentials.php

SQL Statement

$sql = "SELECT * FROM $credentials->userTable WHERE email='$credentials->email'";

./lib/database.php contains the database connection details and connects to the database.

in Log_In.php

include "./lib/user.php";
include "./lib/credentials.php";
include "./lib/database.php";

$userDetails = json_decode($_POST["m"]);
$credentials = new credentials($userDetails);
$sql = "SELECT * FROM $credentials->userTable WHERE email='$credentials->email'";
$result = $conn->query($sql);

//checking the user details
if($result->num_rows === 1) {
    $details = $result->fetch_assoc();      
    $validate = password_verify($credentials->password,$details["userPassword"]);

        if($validate === TRUE){
            if($login_record === TRUE){
                $user = new user($details);
                $user->userType = $credentials->userType;
                $user->loggedInStatus = true;
                echo json_encode($user);
        else {
            echo "error";


In credentials.php

class credentials {

    public function __construct ($credentials) {

        $this->email = $credentials->email;
        $this->password = $credentials->password;
        $this->userType = $credentials->userType;
        $this->userTable = $this->selection ($credentials->userType);

    public function selection ($userType) {
        if ($userType === 'driver') {
            return "Driver_Details";

        if ($userType === 'passenger') {
            return "User_Details";


    public function setCookies ($user) {

        foreach ($user as $key => $value) {


in user.php

class user {
    public function __construct ($details) {
         $this->userName = $details["name"];
         $this->userLocation = $details["country"];
         $this->userId = $details["User_ID"];
         $this->loggedInStatus = false;
         if (isset($details["driverVehicle"])) {
             $this->userVehicle = $details["driverVehicle"];


I need to check the type of user and change the tables to use more than one time in different php files.

I'm looking for some expert ideas and suggestions on a better way to change the database table depending on the user type. Mostly because I need to use that on more different php files.

  • 1
    \$\begingroup\$ So you need help changing the functionality of this script? ...that's not what we do here -- we review code that works exactly as required. Of course, I should also say that injecting submission variables into your query is not a secure/stable practice. \$\endgroup\$ Mar 26, 2020 at 23:05
  • 1
    \$\begingroup\$ mickmackusa This code is working as required. I'm looking for some ideas about changing the ´´´´user table´´´´ depending on user type. Using it on many php files a good practice? How should i set submission variables to my sql? \$\endgroup\$ Mar 27, 2020 at 4:42


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.