如何在php中通过email id使用session获取表的另一个数据

06odsfpq  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(356)

我的表名-我的表列

fname lname email password

我想通过电子邮件id或密码使用会话从表中获取fname。我想在登录后在另一个页面上显示fname。请帮帮我。

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");
    }

}

还有另一个页面-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>';

}

电子邮件在另一页上是echo,但fname不显示(echo) Error- Invalid user-name. 如何在另一页上回显fname?请帮帮我。

fae0ux8s

fae0ux8s1#

我更新了你的脚本,现在可以不使用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");
    }
?>

相关问题