简体   繁体   中英

PHP and MySQL Login/Register System Not Working

I am trying to create a registration and login system in PHP with MySQL. It is not working, I am not sure why. I think the database is connecting because no errors surrounding the database appear.

register.php

<?php
require('connect.php');
if (isset($_POST) & !empty($_POST)) {
    $username = mysqli_real_escape_string($_POST['username']);
    $email = mysqli_real_escape_string($_POST['email']);
    $password = md5($_POST['password']);
    $sql = "INSERT INTO `user` (username, email, password) VALUES ('$username', '$email', '$password')";
    $result = mysqli_query($connection, $sql);
    if ($result) {
        echo "User Registeration Successfull";
    } else {
        echo "User Registeration Failed";
    }
}
?>
<html>
    <head>
        <title>
            Register
        </title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container">
            <form class="form-signin" method="POST">
                <?php if (isset($smsg)) { ?><div class="alert alert-success" role="alert">
                    <?php echo $smsg; ?> 
                    </div>
                <?php } ?>
                <?php if (isset($fmsg)) { ?><div class="alert alert-danger" role="alert">
                    <?php echo $fmsg; ?> 
                    </div>
                <?php } ?>
                <h2 class="form-signin-heading">
                    Please Register
                </h2>
                <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">@</span>
                    <input type="text" name="username" class="form-control" placeholder="Username" required>
                </div>
                <label for="inputEmail" class="sr-only">Email address</label>
                <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
                <label for="inputPassword" class="sr-only">Password</label>
                <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="remember-me"> Remember me
                    </label>
                </div>
                <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
                <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
            </form>
        </div>
    </body>
</html>

connect.php

<?php
$connection = mysqli_connect('localhost', 'root', 'G2zfSB7X');
if (!$connection){
    die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'beta');
if (!$select_db){
    die("Database Selection Failed" . mysqli_error($connection));
}
?>

添加mysqli_error()它显示错误消息

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

change your code on button, adding name in button and change condition php like this

 <?php require('connect.php'); if(isset($_POST['save'])){ $username = mysqli_real_escape_string($_POST['username']); $email = mysqli_real_escape_string($_POST['email']); $password = md5($_POST['password']); $sql = "INSERT INTO `user` (username, email, password) VALUES ('$username', '$email', '$password')"; $result = mysqli_query($connection, $sql); if($result){ echo "User Registeration Successfull"; }else{ echo "User Registeration Failed"; } } ?> <html> <head> <title>Register</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" > <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" > <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <form class="form-signin" method="POST" action=""> <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?> <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?> <h2 class="form-signin-heading">Please Register</h2> <div class="input-group"> <span class="input-group-addon" id="basic-addon1">@</span> <input type="text" name="username" class="form-control" placeholder="Username" required> </div> <label for="inputEmail" class="sr-only">Email address</label> <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" name="save">Register</button> <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a> </form> </div> </body> </html> 

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