比较用户提交到数据库的值,根据结果重定向

xtupzzrd  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(315)

我试图建立一个网页,允许用户输入代码(数字和字母只)。如果代码是“赢家”,它会将他们重定向到我的网站上的“赢家”页面。否则,它会将它们重定向到“丢失”页面。我是新手,但我正在努力学习!
html格式:

<h1>Break Open Your Code</h1>
<form target="_blank" action="phpfile.php" method="POST">
    <input type="hidden" name="code" value="1" />
    <label>Enter Your Code Here</label> 
    <input type="text" name="answer" /> 
    <input onclick="window.location.href = 'results-page.html';" type="submit" value="Check your Code" />
</form>

PHP:

<?php
// Connect to your MySQL database
$dbhst = "localhost";
$dbnme = "mydatabase"; 
$bdusr = "myusername";
$dbpws = "mypassword";

// Using PDO to connect

$conn = new PDO('mysql:host='.$dbhst.';dbname='.$dbnme, $bdusr, $dbpws);

// Getting variables
$answer = $_POST['answer'];
$questionID = $_POST['questionID'];

// Comparing answers

try {

    $stmt = $conn->prepare('SELECT * FROM table_with_answers WHERE question='" . $questionID . "' and answer='". $answer . "' LIMIT 0,1');
    $stmt->execute();

    $result = $stmt->fetchAll();

    if ( count($result) ) {
        foreach($result as $row) { 
            echo 'Congrats, you've entered a correct code';
            // Do Something Else
        }
    } else {
        echo 'Your code did not win. Please try again.';
        exit;
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>
jgovgodb

jgovgodb1#

删除 onclick="window.location.href = 'results-page.html';" 和修改 <input type="hidden" name="code" value="1" /><input type="hidden" name="questionID" value="1" /> html格式:

<h1>Break Open Your Code</h1>
<form target="_blank" action="phpfile.php" method="POST">
    <input type="hidden" name="questionID" value="1" />
    <label>Enter Your Code Here</label> 
    <input type="text" name="answer" /> 
    <input type="submit" value="Check your Code" />
</form>

将html文件保存到form.html
现在修改phpfile.php
添加 header("Location: winner.html"); 如果代码正确
添加 header("Location: losing.html"); 如果代码不正确
PHP:

<?php
// Connect to your MySQL database
$dbhst = "localhost";
$dbnme = "mydatabase"; 
$bdusr = "myusername";
$dbpws = "mypassword";

// Using PDO to connect

$conn = new PDO('mysql:host='.$dbhst.';dbname='.$dbnme, $bdusr, $dbpws);

// Getting variables
$answer = $_POST['answer'];
$questionID = $_POST['questionID'];

// Comparing answers

try {

    $stmt = $conn->prepare("SELECT * FROM table_with_answers WHERE question='" . $questionID . "' and answer='". $answer . "' LIMIT 0,1");
    $stmt->execute();

    $result = $stmt->fetchAll();

    if ( count($result) ) {
        foreach($result as $row) { 
            // echo 'Congrats, you've entered a correct code';
            header("Location: winner.html");
        }
    } else {
        // echo 'Your code did not win. Please try again.';
        header("Location: losing.html");
        exit;
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>

创建html winner.html和losing.html
winner.html: Congrats, you've entered a correct code 正在丢失.html: Your code did not win. Please try again.

相关问题