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

时间:2022-08-17 07:19:19

this is the codes I currently have.

这是我目前的代码。

<!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>

So basically I want the user to input the values inside the textbox and then I'll have to validate the textbox by checking the values of the textbox to the database. Anyone can show me the codes on how it should be done? :)

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

I have changed into this checking method, what is wrong with it?

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

<!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 个解决方案

#1


0  

Use This code

使用此代码

$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']));

Look here tutorial http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

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

#1


0  

Use This code

使用此代码

$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']));

Look here tutorial http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

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