如何根据数据库值检查输入文本

时间:2016-01-26 10:50:03

标签: javascript php mysql database validation

这是我目前的代码。

<!DOCTYPE html>
<?php
include ("dbFunctions.php");


$query = "SELECT * FROM ten_eleven_twelve";

$result = mysqli_query($link, $query);

mysqli_close($link);
?>
<html>
<head>
    <title>
        Times Table Game
    </title>

<center>
    <h1>Game Play - HARD</h1>
    <div class="border_solid">
        <div id="timer"></div>
    </div>
    <hr>
</center>

<link rel="stylesheet" href="CSS/hard.css">
</head>

<body>
<?php
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $questID = $row['ten_eleven_twelve_id'];
        $factor1 = $row['factor1'];
        $factor2 = $row['factor2'];
        $answer = $row['answer'];
        ?>
        <form action="demo_form.asp">
            <div class="image-questionid"><u>Question <?php echo $questID; ?></u></div>
            <input type="text" class="factor1" name="factor1"><br>
            <div class="image-betweenfactor">X</div>
            <input type="text" class="factor2" name="factor2"><br>
            <div class="image-questionequals">=</div>
            <div class="image-answer"><?php echo $answer; ?></div>
            <div class="submit"><input type="submit" value="Submit"></div>
        </form>

        <?php
        break;
    }
}
?>
<script type="text/javascript">
    var myVar = setInterval(function () {
        myTimer()
    }, 1000);
    var d = 0;
    function myTimer() {
        document.getElementById("timer").innerHTML = d++;
    }

</script>
</body>
</html>

所以基本上我希望用户在文本框中输入值,然后我必须通过检查文本框到数据库的值来验证文本框。任何人都可以告诉我应该怎么做的代码? :)

我已经改变了这个检查方法,它有什么问题?

<!DOCTYPE html>
<?php
include ("dbFunctions.php");


$query = "SELECT * FROM ten_eleven_twelve";

$result = mysqli_query($link, $query);

mysqli_close($link);
?>
<html>
<head>
    <title>
        Times Table Game
    </title>

<center>
    <h1>Game Play - HARD</h1>
    <div class="border_solid">
        <div id="timer"></div>
    </div>
    <hr>
</center>

<link rel="stylesheet" href="CSS/hard.css">
</head>

<body>
<?php
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $questID = $row['ten_eleven_twelve_id'];
        $factor1 = $row['factor1'];
        $factor2 = $row['factor2'];
        $answer = $row['answer'];
        ?>
        <form method="post" onsubmit="validate()">
            <div class="image-questionid"><u>Question <?php echo $questID; ?></u></div>
            <input type="text" class="factor1" id="factor1" name="factor1"><br>
            <div class="image-betweenfactor">X</div>
            <input type="text" class="factor2" id="factor2" name="factor2"><br>
            <div class="image-questionequals">=</div>
            <div class="image-answer"><?php echo $answer; ?></div>
            <div class="submit"><input type="submit" value="Submit"></div>
        </form>

        <?php
        break;
    }
}
?>
<script type="text/javascript">
    var myVar = setInterval(function () {
        myTimer()
    }, 1000);

    var d = 0;
    function myTimer() {
        document.getElementById("timer").innerHTML = d++;
    }

    function validate() {
        var factor1 = document.getElementById('factor1').value;
        var factor2 = document.getElementById('factor2').value;
        if (factor1 === $row['factor1'] && factor1 === $row['factor2']) {
            var msg = "There is a problem with the Registration form";
            alert(msg);
            return true;
        } else {
            alert(msg);
            return false;
        }
    }
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

使用此代码

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

$stmt = $db->prepare("INSERT INTO table(factor1,factor2) VALUES(:factor1,:factor2)"); 
$stmt->execute(array(':factor1' => $_POST['factor1'],':factor2' => $_POST['factor2']));

在这里查看教程http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers