我在register.php代码中做错了什么?

我在register.php代码中做错了什么?,php,html,mysql,Php,Html,Mysql,我正在尝试使用php和mysql制作注册表。 我使用的是xampp和phpmyadmin,表“user”上的sql查询工作正常。 但当我尝试使用php代码插入时,它显示“用户注册失败”。 下面是我的代码 register.php <?php require('includes/connect.php'); // If the values are posted, insert them into the database. if (isset($_POST['use

我正在尝试使用php和mysql制作注册表。
我使用的是xampp和phpmyadmin,表“user”上的sql查询工作正常。
但当我尝试使用php代码插入时,它显示“用户注册失败”。
下面是我的代码

register.php

<?php
    require('includes/connect.php');
    // If the values are posted, insert them into the database.
    if (isset($_POST['username']) && isset($_POST['psw'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['psw']; 
    $query = "INSERT INTO `user` (username, password, email) VALUES('$username', '$password', '$email')";
        $result = mysqli_query($connection, $query);
        if($result){
            $smsg = "User Created Successfully.";
        }else{
            $fmsg ="User Registration Failed";
        }
        }
      ?>

<html>
<head>
    <title>BondOnNet | Register</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

<!-- JS -->
<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>   
<script src="js/jquery.jcarousel.pack.js" type="text/javascript"></script>  
<script src="js/jquery-func.js" type="text/javascript"></script>    
<!-- End JS -->

<link rel="stylesheet" type="text/css" href="css/register.css">
</head>
<body>
<div id="Register_header">
    <h1 id="logo"><a href="index.html">BondOnNet</a></h1>
</div>
<div id="register_container">
    <form method="POST" style="border:1px solid #ccc">
        <?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 } ?>
      <div class="imgcontainer">
        <img src="images/avatar.png" alt="Avatar" class="avatar">
      </div>
      <div class="container">
        <label><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="username" required>

        <label><b>Email</b></label>
        <input type="text" placeholder="Enter Email" name="email" required>

        <label><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="psw" required>

        <label><b>Repeat Password</b></label>
        <input type="password" placeholder="Repeat Password" name="psw-repeat" required>
        <input type="checkbox" checked="checked"> Remember me
        <p>By creating an account you agree to our <a href="terms.html" style="color: #4CAF50;">Terms & Privacy</a></p>

        <div class="clearfix">
          <button type="submit" class="signupbtn">Sign Up</button>
        </div>
      </div>
    </form>
</div>
</body>
</html>

使用此选项检测错误:

mysqli_connect_errno() - Returns the error code from last connect call
mysqli_connect_error() - Returns a string description of the last connect error
mysqli_errno() - Returns the error code for the most recent function call
mysqli_sqlstate() - Returns the SQLSTATE error from previous MySQL operation

有关更多信息和示例:

echo
您的
$query
变量,并直接在phpmyadmin中复制和运行打印的查询。您将了解查询失败的原因
此外,使用
mysqli\u error($connection)
在执行查询后打印查询的错误,您在查询变量$result的var\u dump()上得到了什么?将它放在if(){}语句中,以确保它也在传递逻辑。您是否尝试将原始查询注入SQL phpmyadmin(如果可用)以查看查询在SQL环境中是否正常运行?请尝试使用而不是引用字符串。这将有助于防止sql注入并转义任何奇怪的字符。表中的其他列是否具有默认值?对于整个社区。为了避免在不知道数据库连接发生了什么的情况下总是出错,请使用错误检测。
mysqli_connect_errno() - Returns the error code from last connect call
mysqli_connect_error() - Returns a string description of the last connect error
mysqli_errno() - Returns the error code for the most recent function call
mysqli_sqlstate() - Returns the SQLSTATE error from previous MySQL operation