我正在研究“忘记用户名”系统我在两个不同页面中使用两种表单,因此代码如下:-
恢复页面.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>
its php code : // 检查电子邮件是否存在于数据库中的代码
<?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 无法获取这个值呢?!