0

您好,我的功能有一个简单的问题,有 2 个代码:

当我输入正确的答案时我需要 echo $row['answer']; 并在下一个代码中成功。

这意味着我需要当 $_POST['answer'] = $row['answer']; 回显我的密码

如果条件允许,有人可以帮我吗?

这段代码运行完美:

<?php 


$username = $_POST['username']; 


include('config.php');
$result = mysqli_query($con,"SELECT * FROM persons WHERE username='$username'");

while($row = mysqli_fetch_array($result)){

echo $row['username'];
echo "</br>";
echo "</br>";
echo "<p><b>Secret Question</b></p>";
echo $row['secret'];
$freeanswer = $row['answer'];
}  
?>

同一页面中的代码问题:

</br>
</br>
<form action="" method="POST">
<p><b>Answer is :</b><p>
<input type="text" name="answer">
</br>
</br>
<input type="Submit" value="Submit">
</form>
</div>

 <?php 
$anme = $_POST['answer'];
if ($anme==$freeanswer) {
echo "sucess"; 
} else {
echo "wrong";
}

?>
4

3 回答 3

0

你应该把一个名字提交按钮,并检查是否存在 $_POST['name_of_button'];

这是我快速写的一个肮脏的例子,所以你可以明白,我没有测试它

<?php
    // if the button has not been sent show the form
    if(!isset($_POST['submit'])) {
?>
    <form action="/test.php" method="POST">
        <p><b>Answer is :</b><p>
        <input type="text" name="answer">
        </br>
        </br>
        <input type="Submit" value="Submit" name="submit">
    </form>
    </div>
 <?php
// otherwise means the form has been sent, show the password or wrong
    } else {
        include('config.php');
        $result = mysqli_query($con,"SELECT * FROM persons WHERE username='$username'");
        $row = mysqli_fetch_array($result);
        if($row) {
            if($row['answer'] == $_POST['answer']) {
                echo 'your password is ' . $row['password'];
            } else {
                echo 'wrong!';
            }
        }
    }
于 2013-08-10T21:34:22.200 回答
0

发送数据有问题吗?如果要将表单数据发布到同一页面,可以使用:

<form action="<?php echo $_SERVER['REQUEST_URI'];?>" method="post">

此代码将捕获您发布的任何内容。(和表格放在同一页)

 <?php 
 if(isset($_POST['answer'])) {;
     if($name==$freeanswer) {
         echo "success"; 
     } else {
         echo "wrong";
     }
 } else {
     // Just for testing
     var_dump("No data sent yet");
 }
 ?>
于 2013-08-10T21:34:30.833 回答
0

第二个代码中声明的$freeanswer变量在哪里?

这个if语句失败,因为没有声明$freeanswer

$anme = $_POST['answer'];
if ($anme==$freeanswer) { ..

我看到你在第一个代码中声明了它,但我在第二个代码中没有看到它..

编辑 :

实际上我没有看到这都是相同的代码。但问题仍然是$freeanswer因为它没有在公共范围内声明它只是在while循环的范围内

尝试在这里声明/编写这行代码:

$username = $_POST['username']; // Near this line
$freeanswer = ""; // Declare this here
于 2013-08-10T21:35:04.107 回答