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

Comparing value submitted by user to database, redirecting based on results

我正在尝试构建一个允许用户输入代码(仅限数字和字母)的网页。根据代码是否为“获胜者”,它会将他们重定向到我网站上的“获胜者”页面。否则,它会将他们重定向到“丢失”页面。我是新手,但我正在努力学习!

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();
}
?>

删除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

losing.html : Your code did not win. Please try again.