简体   繁体   English

如何在PHP中使用会话通过电子邮件ID获取表的其他数据

[英]How can get another data of table using session by email id in PHP

My table name- persons my table columns 我的表格名称-代表我的表格列

    fname lname email password 

I want to get fname from table using session by email id or password. 我想使用会话通过电子邮件ID或密码从表中获取fname。 I want to display fname on another page after login. 我想在登录后在另一页上显示fname。 please help me . 请帮我 。

session_start();

$cser = mysqli_connect("localhost", "root", "", "srptech") or die("connection failed:" . mysqli_error());

if (isset($_REQUEST['login'])) {
    $c = $_REQUEST['fname'];
    $a = $_REQUEST['email'];
    $b = $_REQUEST['password'];

    $res = mysqli_query($cser, "select fname from persons where email='$a'and password='$b'");

    while ($row = mysql_fetch_array($res)) {
        $fname = $row['fname'];

        $_SESSION["user-name"] = $fname;

    }

    $result = mysqli_num_rows($res);
    if ($result == true) {
        $_SESSION["login"] = "1";

        $_SESSION["email-id"] = $a;

        header("location:my-account.php");
        exit();
        session_write_close();
    } else {
        header("location:index.php?err=1");
    }

}

And another page - my-account.php 另一页-my-account.php

session_start();

if (!isset($_SESSION["login"])) // $_SESSION is a variable and login is a SESSION name
{
    header("location:index.php");
} else {
    echo 'Welcome' . '<h3>' . $_SESSION["email-id"] . '</h3>';
    echo 'Welcome' . '<h3>' . $_SESSION["user-name"] . '</h3>';

}

Email is echo on another page but fname is not displayed (echo) 电子邮件在另一页上回显,但未显示fname(回显)

Error- Invalid user-name.

How can echo fname on another page? 如何在另一页上回显fname? Please help me. 请帮我。

I updated your script, now it's safe to use no SQL injection possible 我更新了脚本,现在可以安全使用SQL注入了

<?php
    if (isset($_REQUEST['login']))
    {
        $Email = $_REQUEST['email'];
        $Password = $_REQUEST['password'];

        $DB = new mysqli("localhost", "root", "", "srptech");

        if ($DB->connect_errno)
        {
            printf("Connect failed: %s\n", $DB->connect_error);
            exit();
        }

        $STMT = $DB->prepare("SELECT `fname` FROM `persons` WHERE `email` = ? AND `password` = ? LIMIT 1");
        $STMT->bind_param("ss", $Email, $Password);
        $STMT->execute();
        $STMT->bind_result($FirstName);
        $STMT->fetch();
        $STMT->close();

        if ($FirstName)
        {
            session_start();

            $_SESSION["user-name"] = $FirstName;
            $_SESSION["login"] = "1";
            $_SESSION["email-id"] = $Email;

            header("location:my-account.php");
            exit();
        }

         header("location:index.php?err=1");
    }
?>

暂无
暂无

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

相关问题 通过使用 php 比较 id 从另一个表中获取数据 - Get data from another table by just comparing the id using php 如何使用 php 和 mySQL 获取客户 ID 并将其插入另一个表 - How to get customer ID and insert it into another table using php and mySQL 如何使用user_id从另一个表获取数据 - how to get data from another table using user_id 如何使用已记录的用户会话ID将数据从一个表获取到另一个表 - How to fetch data from one table to another using the logged user session id 如何使用会话ID从PHP mysql中的两个或多个表中获取值 - how to get values form two or more table in php mysql using session id 如何使用另一个表中的ID从另一个表中获取数据 - How to get data from another table using ID from another table 如何使用PHP在另一个表中使用ID获取一个表的名称 - How to get the name of one table using the id in another table, using PHP 如何使用$ _SESSION [user_id]变量从用户表中提取电子邮件地址并将电子邮件发送到该地址? - How to pull email address form user table and send email to that address using $_SESSION[user_id] variable? SQL / PHP-使用PDO和表中数据的INSERT语句显示为“资源ID#”。 如何获取它以存储正确的字符串? - SQL/PHP - INSERT statement using PDO and data inside the table appears as 'Resource ID #'. How can I get it to store the correct string? 如何在php中获取会话ID或用户名? - How Can I get a Session Id or username in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM