PHP仅向经过身份验证的用户提交数据库表

时间:2019-04-10 13:27:31

标签: php mysql phpmyadmin localhost

编辑:该代码现在有效。在下面的初始源代码中添加了一些代码。

因此,我有一个名为有效负载的数据库,其中有两个表。一个命名为用户,另一个命名为tohere。这两个表只有两列。用户名和密码。 tohere表为空。但是,users表具有两个填充行。第一行分别以admin和admin123作为用户名和密码。第二行分别将visitor和visitor123作为用户名和密码。有一个带有两个输入字段的HTML表单,用户可以在其中输入用户名和密码。现在的想法是,如果他们在表单中输入的用户名和密码与users表中的记录匹配,我希望将那些内容提交到当前为空的tohere表中。我该怎么做?

我尝试了以下似乎无效的代码;

<html>
    <head>
        <title>
        </title>
<?php
$message     = "";
$theusername = $_POST['userName'];
$thepass     = $_POST['password'];
if (count($_POST) >
    0) {
    $conn   = mysqli_connect("localhost", "root", "", "payload");
    $result = mysqli_query($conn, "SELECT * FROM users WHERE     userName='" . $_POST["userName"] . "' and password = '" . $_POST["password"] . "'");
    $count  = mysqli_num_rows($result);
    if ($count == 0) {
        $message = "Invalid Username or Password!";
    } else {

        $sqltwo = "INSERT INTO tohere (userName, password) VALUES ('$theusername', '$thepass')";
//The below if statement was missing.
if(!mysqli_query($conn, $sqltwo)){
    echo "Failed!";
}
else{
    echo "Success!";
}
    }
}
?>
</head>
    <body>
        <form action="" method="post" name="frmUser">
            <div class="message">
                <?php if ($message != "") {echo $message;}?>
            </div>
            <table align="center" border="0" cellpadding="10" cellspacing="1" class="tblLogin" width="500">
                <tr class="tableheader">
                    <td align="center" colspan="2">
                        Enter Login Details
                    </td>
                </tr>
                <tr class="tablerow">
                    <td>
                        <input class="login-input" name="userName" placeholder="User Name" type="text"/>
                    </td>
                </tr>
                <tr class="tablerow">
                    <td>
                        <input class="login-input" name="password" placeholder="Password" type="password"/>
                    </td>
                </tr>
                <tr class="tableheader">
                    <td align="center" colspan="2">
                        <input class="btnSubmit" name="submit" type="submit" value="Submit"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

到目前为止,此代码有效,但仅部分有效。当我不在用户表中输入用户名和密码时,会收到消息“无效的用户名或密码!”,这很好,但是当我输入用户表中的记录时,表单不会提交到此表。实际上,什么也没发生。只是一个空白的白色屏幕。有什么作用?

0 个答案:

没有答案