简体   繁体   中英

mysql_query("INSERT INTO… is not working

I'm trying to create a registration page. The page is successfully connected to phpMyAdmin database but it does not echo anything when i click the register button.

<html>
<head>
</head>

<body>

<?php
INCLUDE "connect.php";
INCLUDE "functions.php";
INCLUDE "titlebar.php";
?>

            <div id="loginform">    
            <h1>Register</h1>

            <form name="Register" action="register.php" method="post">
            <?php
            if(isset($POST["submit"])){
                    $username = $_POST["username"];
                    $password = md5 ($_POST["password"]);
                    if(empty($username) or empty($password)){
                            echo "<p>Fields Empty!</p>";
                    } else {
                            mysql_query("INSERT INTO login VALUES('',$username','$password','2','')");
                            echo "<p>Successfully Registered!</p>";
                    }
            }
            ?>
                <p>     
                    <label for="username">Username: </label>
                    <input type="text" name="username" id="username"/></p><p>

                    <label for="password">Password: </label>
                    <input type="password" name="password" id="password"/></p><p>
                    <input type="submit" name="submit" value="Register" />
                </p>
            </form>
            </div>
</body>

The problem is with the post method. use $_POST instead of $POST

You have mysql error

Not: $username'

but '$username'

And next time display mysql errors with mysql_error().

At the beginning, I am not sure what isset($_POST['submit'] should return, but as already mentioned in the comments you missed a single quote.

Additionaly i would use:

$password = password_hash($_POST['password'], 

md5 is deprecated and thus not safe. If you write a login script you can use password_verify(plainPW, hashPW)

You also need to specify a database and login into it. I recommend to look at the W3 Schools examples they are very in-depth and have good examples. W3 school mysqli page

also write a die() at the end of your script and do not foregt to close the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM