注册表工作但显示错误

but5z9lq  于 2021-06-21  发布在  Mysql
关注(0)|答案(5)|浏览(311)

我做了一张登记表。它实际上可以工作,但它显示“注册失败,请重试”消息。这是我的密码。
html和php

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <form action="#" method="POST">
        <h3>Register</h3>
        Username: <input type="text" name="uname"><br>
        Password: <input type="password" name="pass"><br>
        <input type="submit" name="btn">
    </form>
</body>
</html>
<?php
    if (isset($_POST['btn'])) {
        $username = $_POST['uname'];
        $password = $_POST['pass'];

        mysql_connect("localhost","root","");
        mysql_select_db("login2");

        $query = "INSERT INTO users VALUES ('','$username','$password')";

        mysql_query($query);
        echo "Registered Successfully";
    }
    else {
        echo "Failed To Register, Try Again";
    }
?>
bq9c1y66

bq9c1y661#

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <form action="#" method="POST">
        <h3>Register</h3>
        Username: <input type="text" name="uname"><br>
        Password: <input type="password" name="pass"><br>
        <input type="submit" name="btn">
    </form>
</body>
</html>
<?php
    if (isset($_POST['btn'])) 
    {
        $username = $_POST['uname'];
        $password = $_POST['pass'];

        $servername = "localhost";
        $username = "username";
        $password = "password";
        $dbname = "dbname";

        // Create connection
        $conn = new mysqli($servername, $username, $password,$dbname);
        if ($conn->connect_error) 
        {
            die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "INSERT INTO users(`username`, `password `)  VALUES ('$username','$password ')";
        $result = $conn->query($sql);
        if (!$result)
        {
        echo("Error description: " . mysqli_error($conn));
        }
        else
        {
          echo "successfully inserted";
        }
    }
?>
b4wnujal

b4wnujal2#

<?php
    if (isset($_POST['uname']) && !empty($_POST['uname']) && isset($_POST['pass']) && !empty($_POST['pass'])) {
        $username = $_POST['uname'];
        $password = $_POST['pass'];
        ............................
    }
    else {
        echo "Failed To Register, Try Again";
    }
?>
jjjwad0x

jjjwad0x3#

1.if语句在页面加载时显示注册失败,这是一个问题
mysql已弃用,请使用 mysqli_* .
这是一个更新的代码

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <form action="#" method="POST">
        <h3>Register</h3>
        Username: <input type="text" name="uname"><br>
        Password: <input type="password" name="pass"><br>
        <input type="submit" name="btn" value="Submit">
    </form>
</body>
</html>
<?php
    if (isset($_POST['btn'])) {
        $username = $_POST['uname'];
        $password = $_POST['pass'];

        mysqli_connect("localhost","root","");
        mysqli_select_db("login2");

        $query = "INSERT INTO users VALUES ('','$username','$password')";

        $results=mysqli_query($query); 
        if(mysqli_affected_rows($con)>0){ //check if inserted
            echo "Registered Successfully";
        }
        else{
            echo "Failed To Register, Try Again";
        }
    }
?>
mgdq6dx1

mgdq6dx14#

尝试以下代码并检查

<?php
   if (isset($_POST['btn'])) {
    $username = $_POST['uname'];
    $password = $_POST['pass'];

    mysql_connect("localhost","root","");
    mysql_select_db("login2");

    $query = "INSERT INTO users VALUES ('','$username','$password')";

    if(mysql_query($query))
    {
    echo "Registered Successfully";
    }

    else 
    {
    echo "Failed To Register, Try Again";
    }
    }
    ?>
mfuanj7w

mfuanj7w5#

你可以查一下邮局。

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}

同时添加uname和pass控件。

相关问题