Как отобразить зарегистрированное имя пользователя в моем проекте oop php? - PullRequest
0 голосов
/ 28 ноября 2018

Когда я пытаюсь отобразить имя пользователя из моего авторизованного пользователя в моем проекте, я получаю эту ошибку Неопределенная переменная: user_id в C: \ xampp \ htdocs \ login1 \ index.php в строке 2 Запрос не выполнен !!!ошибка в вашем синтаксисе SQL;проверьте руководство, соответствующее вашей версии сервера MariaDB, чтобы найти правильный синтаксис для использования рядом с '' в строке 1.Вот мой код:

index.php:

    <?php require_once("includes/header.php"); ?>
    <?php $user = User::find_user_by_id($user_id); ?>

    <?php 

        if (!$session->is_signed_in()) {
        redirect("login.php");
        }

     ?>

    <div class="row">
       <div class="col-md-12">
           <h1>Home</h1> 

           <h1>Hello, <?php echo $user->username; ?></h1>

    </div>

    <?php require_once("includes/footer.php"); ?>

user.php:

    class User
    {
    public $id;
    public $username;
    public $password;
    public $first_name;
    public $last_name;

    private function has_the_attribute($the_attribute)
    {
       $object_properties = get_object_vars($this);
       return array_key_exists($the_attribute, $object_properties);
    }

    public static function instantation($the_record)
    {
       $the_object = new self;
       foreach ($the_record as $the_attribute => $value) {
         if ($the_object->has_the_attribute($the_attribute)) {
             $the_object->$the_attribute = $value;
            }
          }
        return $the_object;
    }

    public static function find_this_query($sql)
    {
        global $database;
        $result_set = $database->query($sql);
        $the_object_array = [];
        while ($row = mysqli_fetch_array($result_set)) {
        $the_object_array[] = self::instantation($row);
        }
        return $the_object_array;
    }

    public static function find_all_users()
    {
        return self::find_this_query("SELECT * FROM users");
    }

    public static function find_user_by_id($user_id)
    {
        global $database;
        $the_result_array = self::find_this_query("SELECT * FROM users WHERE id=$user_id");
        return !empty($the_result_array) ? array_shift($the_result_array) : false;
        }

    public static function verify_user($username, $password)
    {
        global $database;
        $username = $database->escape_string($username);
        $password = $database->escape_string($password);

        $sql = "SELECT * FROM users WHERE ";
        $sql .= "username = '{$username}' ";
        $sql .= "AND password = '{$password}'";

        $the_result_array = self::find_this_query($sql);
        return !empty($the_result_array) ? array_shift($the_result_array) : false;
        }
  }

    $user = new User();

session.php:

   <?php

   class Session
   {
       private $signed_in = false;
       public $user_id;
       public $message;

       public function __construct()
       {
          session_start();
          $this->check_the_login();
          $this->check_message();
       }

       public function login($user)
       {
          if ($user) {
          $this->user_id = $_SESSION['user_id'] = $user->id;
          $this->signed_in = true;
          }
       }

       public function logout()
       {
          unset($_SESSION['user_id']);
          unset($this->user_id);
          $this->signed_in = false;
       }

       private function check_the_login()
       {
          if (isset($_SESSION['user_id'])) {
          $this->user_id = $_SESSION['user_id'];
          $this->signed_in = true;
          } else {
          unset($this->user_id);
          $this->signed_in = false;
          }
       }

       public function is_signed_in()
       {
          return $this->signed_in;
       }

       public function message($msg="")
       {
          if (!empty($msg)) {
          $_SESSION['message'] = $msg;
          } else {
          return $this->message;
          }
        }

        public function check_message()
        {
          if (isset($_SESSION['message'])) {
          $this->message = $_SESSION['message'];
          unset($_SESSION['message']);
          } else {
          $this->message = "";
        }
     }
  }

  $session = new Session();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...