将我的php登录屏幕连接到我的SQL DB

时间:2016-06-03 09:27:33

标签: php mysql user-interface login-page

我自己并没有真正做多少PHP,但我正在为我正在构建的UI创建一个php登录屏幕,以便从mysql数据库中将数据提取到一个表中。我有表格连接到数据库正常和一般屏幕工作,但我遇到登录屏幕的问题。

目前我想要的是login.php页面接受正确的凭据并将用户引导到index.php页面。

index.php页面包含

include('session.php');    //include login session

例如,我不断收到以下两个错误

  1. 注意:未定义索引:活动

  2. 致命错误:调用未定义的函数session_register()

  3. 以下是我的login.php页面的代码

    <?php
    
       include("config.php");
       session_start();
       $error=''; // Variable To Store Error Message
    
       if($_SERVER["REQUEST_METHOD"] == "POST") {
          // username and password sent from form 
    
          $myusername = mysqli_real_escape_string($db,$_POST['username']);
          $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
    
          $sql = "SELECT user_id FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'";
          $result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));
    
          $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
    
          $active = $row['active'];
    
          $count = mysqli_num_rows($result);
    
          // If result matched $myusername and $mypassword, table row must be 1 row
    
          if($count == 1) {
             // Register $myusername, $mypassword and redirect to file "index.php"
             session_register("username");
             session_register("password");
             $_SESSION['login_user'] = $myusername;
    
             header("location: index.php");
          }else {
             $error = "Your Username Name or Password is invalid";
          }
       }
    ?>
    

    此处还有我的config.php

    的详细信息
    $mysql_hostname = "localhost";  //your mysql host name
    $mysql_user = "adminTest";          //your mysql user name
    $mysql_password = "badgerBadger";           //your mysql password
    $mysql_database = "mushroom";   //your mysql database
    
    $db = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
    //mysql_select_db($mysql_database, $bd) or die("Error on database connection");
      if($db->connect_errno > 0){
          die('Unable to connect to database [' . $db->connect_error . ']');
      }
    

    这是我的session.php文件的代码

    <?php
    
       include('config.php');
       session_start();
    
       $user_check = $_SESSION['login_user'];
       $ses_sql = mysqli_query($db,"SELECT * FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'");
       $login_session =$row['first'];
       $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
    
       $login_session = $row['username'];
    
       if(!isset($_SESSION['login_user'])){
          header("location:login.php");
       }
    
    ?>
    

    任何帮助都将非常感谢,提前感谢:)

    变化

    更新了代码以删除与表查询相关的拼写错误,并解决了其他一些问题。

    更新了login.php代码以进行正确的通话

    新错误#######

    1. session.php中未定义的变量用户名
    2. session.php中未定义的可变密码
    3. session.php第22行中未定义的变量行
    4. 奇怪的是解析变量并重定向到index.php页面。我试过做的是将这些变量的值设置为空白或$ myusername&#34; &#34 ;;例如。这确实消除了用户名和密码的错误,但它仍然抱怨session.php文件中的第24行= $ login_session = $ row [&#39; first&#39;]; 抱怨它是一个非法的字段串,当它实际上是一个有效的字段时。

      TABLENAME admin_tbl ###下面的字段

      • USER_ID
      • t_date
      • 第一
      • 最后
      • 用户名
      • 密码
      • 电子邮件
      • 活性

4 个答案:

答案 0 :(得分:0)

在配置

中使用此代码
$db= mysqli_connect("localhost", "adminTest", "badgerBadger","mushroom")or die("cannot connect...."); 

答案 1 :(得分:0)

确保您的数据库字段名称与查询字段相同,并且数据库中至少有一条记录...并且只检查您的数据库连接

     $result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));

执行此操作并检查错误位置

答案 2 :(得分:0)

Nick, here is your correct code. 
1. Notice: Undefined index: active - error because of you are try to fetch only user_id from your database.so you have to use "SELECT * FROM " or "SELECT user_id,active FROM "
2. Fatal error: Call to undefined function session_register() - you can login and create session without use of session_register() no need of session register
session_start();
   include("config.php");
   $error=''; // Variable To Store Error Message
   $active = '';
   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
      $sql = "SELECT * FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'";
      //$sql = "SELECT user_id,active FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));

      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);

      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         // Register $myusername, $mypassword and redirect to file "index.php"
         //session_register("username");
         //session_register("password");
         $_SESSION['login_user'] = $myusername;

         header("location: index.php");
      }else {
         $error = "Your Username Name or Password is invalid";
      }
   }

答案 3 :(得分:0)

Nick,这是“session.php”的新代码。

session_start(); // always start session before write any code.its good to       start session before database connection
include('config.php');
if(!(isset($_SESSION["login_user"]) && isset($_SESSION["password"]))){
    include("login.php");
}
else{
    $myusername=$_SESSION['login_user'];
    $mypassword=$_SESSION['password'];
}
$ses_sql = mysqli_query($db,"SELECT * FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'");
$row = mysqli_fetch_array($ses_sql);
$login_session =$row['first'];
//$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); "always define variable before using that variable  "

$username_session = $row['username'];

And also after this line 
$_SESSION["login_user"] = $myusername; 
you have to add following code in login.php

$_SESSION["password"] = $row['password'];

BTW its not good practice to use password on session.