无法使用带有sql语句的隐藏字段

时间:2013-10-02 09:54:29

标签: php mysql multipage

我正在制作一个“忘记的用户名”系统我在两个不同的页面中使用两种形式,因此代码如下: -

recover_page.php:

  <form action="security.php" method="post" enctype="multipart/form-data">
    Please Enter your email address:<br>
    <input type="text" name="email" value="<?php $_POST['email']?>">
    <input type="submit" value="submit"> 
    </form>

它的PHP代码:

<?php
include "session.php";
include "database/db.php";
$mode_allowed = array('username','password');
if(isset($_GET['mode']) === false && in_array($_GET['mode'],$mode_allowed)===false){
header('location:index.php');
}
?>

现在是第二页(security.php):

    <form action="security.php" method="POST" enctype="multipart/form-data">
        <p> Answer this question <p>
    <select type="text" selected="selected" name="security_question" value="<?php $security_question?>">
                            <option name="security_question" value="<?php $security_question =mysql_query("SELECT `security_question` FROM `users` WHERE `email`='".mysql_real_escape_string($_POST['email'])."' ");
    $array = mysql_fetch_array($security_question);
    echo $array[0];
    ?>">
    <?php $security_question =mysql_query("SELECT `security_question` FROM `users` WHERE `email`='".mysql_real_escape_string($_POST['email'])."' ");
    $array = mysql_fetch_array($security_question);echo $array[0]; ?>                  
    </option> </select> <br>
    <input type="text" name="answer"/> <br>
    <input type="submit" value="submit"> 
</form>

它的PHP代码: //用于检查数据库中是否存在电子邮件的代码,或者没有

<?php
    include "session.php";
    include "database/db.php";

        $mode_allowed = array('username','password');
            if(isset($_POST['email']) === true && empty($_POST['email']) === false){
                if(email_exists($_POST['email']) === false){
                    echo "Sorry, we can't find this email";
                    exit();
                }
            }
    ?>

//代码搜索数据库中的答案,并将其与用户在“回答”字段中输入的答案进行比较

   <?php
            echo "<input type='hidden' name='email' value=' '".$_SESSION['email']."' '>";
        if(isset($_POST['answer'])){
            $answer = $_POST['answer'];
            if(!empty($answer)){
                $sql = mysql_query("SELECT `username` FROM `users` WHERE `email` ='".mysql_real_escape_string($_SESSION['email'])."' AND `answer`='".mysql_real_escape_string($answer)."'");
                        if(mysql_num_rows($sql) == 1){
                        header('location:last.php?success');                
                    }else {
                        echo "Wrong answer";
                    }

                }else{
                    echo "<script type='text/javascript'>alert('you must answer this question');</script>";
                }
            }
        ?>

现在,当我编写没有隐藏输入的sql语句时,这样可以正常工作: -

$sql = mysql_query("SELECT `username` FROM `users` WHERE `answer`='".mysql_real_escape_string($answer)."'");

而且,我可以回显$_SESSION['email'];,它会给我正确的隐藏字段值。那么为什么sql无法获得这个值?!

1 个答案:

答案 0 :(得分:0)

编辑最后一个脚本的代码(将代码和html放在一起),如下所示: -

<?php
include "session.php";
include "database/db.php";

$Message = "";

$mode_allowed = array('username','password');
if(isset($_POST['email']) === true && empty($_POST['email']) === false)
{
    if(email_exists($_POST['email']) === false)
    {
        echo "Sorry, we can't find this email";
        exit();
    }
}
if(isset($_POST['answer']))
{
    $answer = $_POST['answer'];
    if(!empty($answer))
    {
        $sql = mysql_query("SELECT `username` FROM `users` WHERE `email` ='".mysql_real_escape_string($_SESSION['email'])."' AND `answer`='".mysql_real_escape_string($answer)."'");
        if(mysql_num_rows($sql) == 1)
        {
            header('location:last.php?success');                
        }
        else 
        {
            $Message = "Wrong answer";
        }
    }
    else
    {
        echo "<script type='text/javascript'>alert('you must answer this question');</script>";
    }
}
?>
<form action="security.php" method="POST" enctype="multipart/form-data">
    <p> Answer this question </p>
    <select type="text" selected="selected" name="security_question" value="<?php $security_question?>">
        <option name="security_question" value="<?php $security_question =mysql_query("SELECT `security_question` FROM `users` WHERE `email`='".mysql_real_escape_string($_POST['email'])."' ");
        $array = mysql_fetch_array($security_question);
        echo $array[0];
        ?>">
        <?php $security_question =mysql_query("SELECT `security_question` FROM `users` WHERE `email`='".mysql_real_escape_string($_POST['email'])."' ");
        $array = mysql_fetch_array($security_question);echo $array[0]; ?>                  
        </option> 
    </select> <br>
    <input type="text" name="answer"/> <br>
    <input type='hidden' name='email' value='<?php $_POST['email']?>'>
    <input type="submit" value="submit"> 
    <?php if ($Message != '') echo "<br /> $Message";?>
</form>