PHP 和 MySQL Login/Register 系统不工作

PHP and MySQL Login/Register System Not Working

我正在尝试使用 MySQL 在 PHP 中创建一个注册和登录系统。 它不起作用,我不确定为什么。我认为数据库正在连接,因为没有出现数据库周围的错误。

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));
}
?>

更改按钮上的代码,在按钮中添加名称并更改条件 php 像这样

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

添加mysqli_error() 显示错误信息

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