Php 未定义索引-注册表窗体

Php 未定义索引-注册表窗体,php,html,mysql,server,Php,Html,Mysql,Server,我试图创建一个注册页面,因此当用户填写相关的详细信息时,它会将它们注册到MYSQL中的数据库并存储详细信息 在他们可以继续登录之前,我遇到的错误是“未定义索引”显示在所有变量上-用户名、密码、名字、姓氏和电子邮件 我已经将所有这些变量添加到后端数据库中,但仍然没有添加任何变量 HTML代码: <?php $mysqli = mysqli_connect('localhost', 'test', 'test123', 'test'); if ($mysqli->conne

我试图创建一个注册页面,因此当用户填写相关的详细信息时,它会将它们注册到MYSQL中的数据库并存储详细信息

在他们可以继续登录之前,我遇到的错误是“未定义索引”显示在所有变量上-用户名、密码、名字、姓氏和电子邮件

我已经将所有这些变量添加到后端数据库中,但仍然没有添加任何变量

HTML代码:

    <?php

$mysqli = mysqli_connect('localhost', 'test', 'test123', 'test'); 

if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; 
    exit();
}

global $mysqli;
$username   = $_POST['username']; 
$password   = $_POST['password'];
$firstname  = $_POST['firstname'];
$lastname   = $_POST['lastname'];
$email      = $_POST['email'];

$exists = 0;
$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1"); 
if ($result->num_rows == 1) { 
    $exists = 1; 
    $result = $mysqli->query("SELECT password from users WHERE password = '{$password}' LIMIT 1"); 
    if ($result->num_rows == 1) $exists = 2; 
} else {
    $result = $mysqli->query("SELECT email from users WHERE email = '{$email}' LIMIT 1"); 
    if ($result->num_rows == 1) $exists = 3; 
}

if ($exists == 1) echo "<p>Username already exists!</p>"; 
else if ($exists == 2) echo "<p>Username and Email already exists!</p>"; 
else if ($exists == 3) echo "<p>Email already exists!</p>"; 
else {
    # insert data into mysql database
    $sql = "INSERT  INTO `users` (`id`, `username`, `password`, `firstname`, `lastname`, `email`) 
            VALUES (NULL, '{$username}', '{$password}', '{$firstname}', '{$lastname}', '{$email}')";

    if ($mysqli->query($sql)) {
        //echo "New Record has id ".$mysqli->insert_id; 
        echo '<meta http-equiv="refresh" content="0; url=test.html">'; 
    } else {
        echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>"; 
        exit();
    }
}
?>      
试试这个,添加

<body>
    <div class="container">
      <h1 class="text-center">Booking</h1>
      <form class="form-signin" method="POST" action="register.php">
        <h2 class="form-signin-heading">Register</h2>
        <div id="all-form">
        <h3>User Details</h3>
        <input required type="text" class="form-control" placeholder="Name">
        <input required type="text" class="form-control" placeholder="Email Address">
        <h4>Login Details</h4>
        <input required type="text" class="form-control" placeholder="Username">
        <input required type="password" class="form-control" placeholder="Password">
        <input required type="password" class="form-control" placeholder="Confirm Password">
        <button class="btn btn-primary btn-sm" type="register">Register</button>
        <p><a href="signup2.html">Already registered? Sign in here </a></p>
      </form>
      </div>
    </div>
    <!-- /container -->
  </body> 

在php代码的顶部。检查您的控制台日志,看看会出现什么情况。当我这样做时,我发现post数据中存在一些非常奇怪的行为。

您必须在输入标记中添加一个name属性,然后才能使用$\u post[]数组访问它们。您没有在输入标记中添加任何名称属性。参见代码:

     error_log(print_r($_POST,true));
请注意,这里我添加了带有所有输入标记的name属性。现在,无论何时使用$\u POST[]数组访问此名称,都不会出现任何未定义的索引错误

<body>
<div class="container">
  <h1 class="text-center">Booking</h1>
  <form class="form-signin" method="POST" action="register.php">
    <h2 class="form-signin-heading">Register</h2>
    <div id="all-form">
    <h3>User Details</h3>
    <input required type="text" class="form-control" placeholder="Name" name="name">
    <input required type="text" class="form-control" placeholder="Email Address" name="email">
    <h4>Login Details</h4>
    <input required type="text" class="form-control" placeholder="Username" name="username">
    <input required type="password" class="form-control" placeholder="Password" name="password">
    <input required type="password" class="form-control" placeholder="Confirm Password" name="confirm_password">
    <button class="btn btn-primary btn-sm" type="submit">Register</button>
    <p><a href="signup2.html">Already registered? Sign in here </a></p>
  </form>
  </div>
</div>
<!-- /container -->