-2

我的主页上有一个登录脚本,我的登录表单也在我的主页上。当用户向登录提交表单时,他/她提交他/她的用户名和密码。

脚本访问的数据库存储了用户注册时的用户名、密码和电子邮件地址。

一旦用户成功登录,他/她将被重定向到一个页面,该页面加载他们以前在页面上的“评论”,这些评论存储在同一数据库的不同表中。

我需要将电子邮件从一个表发送到重定向页面上的查询。

这是我处理登录的 PHP 代码的代码:

<?php

//If the user has submitted the form
if(isset($_REQUEST['username'])){
    //protect the posted value then store them to variables
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);

    //Check if the username or password boxes were not filled in
    if(!$username || !$password){
        //if not display an error message
        echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
    }else{
        //if they were continue checking

        //select all rows from the table where the username matches the one entered by the user
        $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
        $num = mysql_num_rows($res);

        //check if there was no match
        if($num == 0){
            //if none, display an error message
            echo "<center>The <b>Username</b> you supplied does not exist!</center>";
        }else{
            //if there was a match continue checking

            //select all rows where the username and password match the ones submitted by the user
            $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
            $num = mysql_num_rows($res);

            //check if there was no match
            if($num == 0){
                //if none display error message
                echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
            }else{
                //if there was continue checking

                //split all fields from the correct row into an associative array
                $row = mysql_fetch_assoc($res);

                //check to see if the user has not activated their account yet
                if($row['active'] != 1){
                    //if not display error message
                    echo "<center>You have not yet <b>Activated</b> your account!</center>";
                }else{
                    //if they have log them in

                    //set the login session storing there id - we use this to see if they are logged in or not
                    $_SESSION['uid'] = $row['id'];


                    //update the online field to 50 seconds into the future
                    $time = date('U')+50;
                    mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");

                    //redirect them to the usersonline page
                    echo 'REDIRECT';

                }
            }
        }
    }

    exit;
}

?>

这是重定向到页面上的 PHP 代码:

<?php
  $con=mysqli_connect("","","","");
  // Check connection
  if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $result = mysqli_query($con,"SELECT * FROM comments
    WHERE email='$_POST[email]' ORDER BY dt");

  while($row = mysqli_fetch_array($result))
  {
    echo $row['dt'] ." " . $row['email'] . " " . $row['body'];
    echo "<br>";
    echo "<br>";
  }
?>

我需要在第一个代码中添加一些内容,以便从用于验证登录信息的表中提取电子邮件地址,并将其发送到第二个代码以接收“评论”。我试过用谷歌搜索答案,但一无所获。请帮忙!

4

1 回答 1

0

由于您在代码中使用了该$_SESSION数组(可能是从某处复制的),因此您可以类似地将电子邮件地址存储在同一个数组中。

$_SESSION['email'] = $row['email'];

在后面的页面中,您需要替换$_POST['email']$_SESSION['email'].

于 2013-03-28T07:14:22.240 回答