简体   繁体   English

如何从已登录用户的数据库中提取数据?

[英]How do I pull data from a database from a logged in user?

I will try to keep this post as simple as possible while including as much information as I can to assist you in helping me. 我将尽力使这篇文章尽可能简单,同时尽可能多地提供信息以帮助您。 Just as a forewarning, I am extremely new to PHP/Mysqli and over the course of the last week put together an internal data site for our company to make our jobs easier. 就像一个警告一样,我是PHP / Mysqli的新手,在过去的一周中,我为公司建立了一个内部数据站点,以简化我们的工作。

I have completed the user login process and it seems to be working quite well. 我已经完成了用户登录过程,它似乎运行得很好。 If the user is not logged in, they can not see the PHP pages of the site and are redirected accordingly. 如果用户未登录,则他们将看不到站点的PHP页面,因此将被重定向。 Now that I have this part working. 现在我已经开始工作了。 I am trying to incorporate a site identifier that shows their first and last name as well as a photo of them. 我正在尝试合并一个网站标识符,以显示他们的名字和姓氏以及他们的照片。 The placeholders are already setup but I have not been very successful in getting this to work. 占位符已经设置好了,但是在使它起作用方面并不是很成功。 I did, however, add a "Welcome "Email" message at the time by pulling the session data. 但是,我确实通过拉出会话数据在当时添加了“欢迎使用“电子邮件”消息。

I will list the code that I am currently using below: 我将在下面列出我当前正在使用的代码:

LOGIN PAGE: 登录页面:

<?php
require('../inc/db.inc.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['user_email'])){

    $email = stripslashes($_REQUEST['user_email']); // removes backslashes
    $email = mysqli_real_escape_string($con,$email); //escapes special characters in a string
    $password = stripslashes($_REQUEST['user_pwd']);
    $password = mysqli_real_escape_string($con,$password);

//Checking is user existing in the database or not
    $query = "SELECT * FROM `users` WHERE user_email='$email' and user_pwd='".md5($password)."'";
    $result = mysqli_query($con,$query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
    if($rows==1){
        $_SESSION['user_email'] = $email;
        header("Location: ../index.php"); // Redirect user to index.php
        }else{
            echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
            }
}else{
?>

DB PAGE: 数据库页:

<?php

$con = mysqli_connect("localhost","root","","dbadmin");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

SECURITY ON EACH PAGE: 每个页面上的安全性:

<?php
include("pages/auth.php"); //include auth.php file on all secure pages
?>

AUTH PAGE: 认证页面:

<?php
session_start();
if(!isset($_SESSION["user_email"])){
header("Location: login.php");
exit(); }
?>

CODE USED TO DISPLAY EMAIL: ( only code I was able to get working) 用于显示电子邮件的代码:(只有我可以使用的代码)

<p>Welcome <?php echo $_SESSION['user_email']; ?> 

I hope that one of you Guru types see that and know exactly how to get what I am looking for, or at least point me in the right direction. 我希望你们中的一位Guru类型的人能够看到这一点,并且确切地知道如何获得我想要的东西,或者至少将我指向正确的方向。 I have been searching the internet fir what seems like days reading all of the tutorials that I can find but I have not been able to successfully make anything work. 我一直在搜寻互联网,似乎每天都在阅读我可以找到的所有教程,但未能成功完成任何工作。 I have found the following code but each time I try to add it to my code, it breaks the site. 我已经找到以下代码,但是每次尝试将其添加到我的代码中时,它都会破坏站点。

$row = mysql_fetch_array($result);

Please assist if you are able. 如果可以,请协助。 Thanks in advance. 提前致谢。

Go to your login page where you check if row is equal 1, do this, change this 转到登录页面,检查行是否等于1,执行此操作,然后更改

$_SESSION['user_email'] = $email;

To this 对此

$_SESSION['user'] = $result->fetch_assoc();

Then do this 然后做这个

<p>Welcome <?php echo  $_SESSION['user']['firstname']; ?></p>

Not a direct answer to your questions, but if are new to php, I would advise you to use one of the popular framework of php that already provide what you are looking for, and more, in a secure way, with secured libraries. 不是直接回答您的问题,但是,如果您不熟悉php,我建议您使用一种流行的php框架,该框架已经提供了您想要的内容,并且以安全的方式提供了安全的库。

for example laravel or symfony. 例如laravel或symfony。

Those are very well documented, and you will learn much more checking their documentation and tutorial than trying to reinvent the wheel here. 这些文档都有很好的文档说明,与在此处尝试重新发明轮子相比,您将学到更多有关检查其文档和教程的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM