用户登录时,使用PHP代码从数据库中获取特定数据

时间:2017-02-26 10:20:55

标签: php database

我想用PHP代码从我的数据库中获取数据。简而言之,当用户使用id&密码我想只在用户面板中显示他的数据,但我收到了一些错误:这是我的用户管理面板代码:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>User Panel</title>
    <link rel="stylesheet" href="assets/demo.css">
    <link rel="stylesheet" href="assets/form-labels-on-top.css">

</head>
<body>
    <header>
        <h1 align="center"><b>User Information Details</b></h1></br>    
    </header>




    <?php

            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "xyz";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) 
            {
                die("Connection failed: " . $conn->connect_error);
            } 

            include('user_login_check.php');


            $result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());





    //  $result = mysql_query("SELECT * FROM user_information WHERE user_name='" . $_POST["user_name"] . "' and user_password = '". $_POST["user_password"]."'");

    ?>  

<form>
<table border="5" style= "background-color: #333333; color: #FFF; margin: 0 auto; padding:100px" >
<thead>
<tr>
<th>User ID</th>
  <th>User Type</th>
  <th>User Name</th>
  <th>User Country</th>
  <th>User Email</th>
  <th>User Phone</th>
  <td>User Address</td>
  <td>User Password</td>
  <td>User Status</td>
  <td>Edit</td>

</tr>
</thead>
    <tbody>
        <?php

          while( $row = mysqli_fetch_assoc( $result ) ){
            echo
            "<tr>
              <td>{$row['user_id']}</td>
              <td>{$row['user_type']}</td>
              <td>{$row['user_name']}</td>
              <td>{$row['user_country']}</td>
              <td>{$row['user_email']}</td>
              <td>{$row['user_phone']}</td>
              <td>{$row['user_address']}</td> 
              <td>{$row['user_password']}</td> 
              <td>{$row['user_status']}</td>
              <td><a href=\"edit.php?id={$row['user_id']}\"><img src='image/editicon.jpeg'/></a></td>;

            </tr>\n";



          }
        ?>



    </tbody>
    <div align="left" style="background:#333333">
    <h1><input type="button" value="Log Out" onClick="window.location.href='logout_user.php'" />    </h1>
</div>
 </table>
</form>             
</body>
</html>

问题特别在于本节

$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());

这是我的登录检查代码:

<?php
 //connect the database
$hostName="localhost";
$dbUsername="root";
$dbPassword="";
$dbName="xyz";
mysql_connect($hostName,$dbUsername,$dbPassword) or die("Connection failed");
mysql_select_db($dbName) or die("Database name doesn't exist");

//start session
@session_start();

if(isset($_POST["user_name"] , $_POST["user_password"])) 
        {     
           $User_name = $_POST["user_name"]; 
          // echo "$User_name";
           $User_password = $_POST["user_password"]; 
         //  echo "$User_password";
            $sql = "SELECT * FROM `user_information` WHERE `user_name`='".$User_name."' AND `user_password`='".$User_password."' ";
            $result = mysql_query($sql) or trigger_error(mysql_error().$sql);        
            // $result = mysql_query("SELECT * FROM `user_info` WHERE `User_name`='".$username."' AND `User_password``='".$password."'");

            $my_arary = array();
            while($row = mysql_fetch_assoc($result))
                {
                    $_SESSION['id']= $row["Id"];
                //  echo $_SESSION['login_user_id'];
                    $_SESSION['user_password']= $row["user_password"];
                    //echo $_SESSION['login_user_password'];
                    $_SESSION['user_name']= $row["user_name"];
                    //echo $_SESSION['login_user_name'];


                    $my_arary[] = $row;
                    print_r($row);

                    if($User_name == $_SESSION['user_name'] && $User_password == $_SESSION['user_password']){
                    header("Location: userpanel.php"); 
                    //echo "successfully logged in";
                    }

                    else{
                    echo "no matches";
                     header("Location: user_login_basic.php");
                    }

                }
        } 
?>

1 个答案:

答案 0 :(得分:0)

您设置了$_SESSION['id']= $row["Id"]。您确定表格中有Id列吗?因为您在用户管理面板代码

中的查询中使用了user_id而不是Id