为什么HTML页面无法读取php页面

时间:2019-10-12 09:59:39

标签: php html mysql bootstrap-4

当我单击“提交”按钮时,我没有遇到任何问题,并且我的数据没有插入数据库中。

我正在尝试创建一个注册页面(register2.html)和db_reg.php以将数据发送到数据库中。

但是我发现Html页面甚至没有读取PHP页面。

db_reg.php

<?php
session_start();

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'mdff');

// initializing variables
$fullName = $_POST['fullName'];
$email    = $_POST['email'];
$phoneNum = $_POST['phoneNum'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
$errors = array(); 


// REGISTER USER
if (isset($_POST['register2'])) {
  // receive all input values from the form
  $fullName = mysqli_real_escape_string($db, $_POST['fullName']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $phoneNum = mysqli_real_escape_string($db, $_POST['phoneNum']);
  $password1 = mysqli_real_escape_string($db, $_POST['password1']);
  $password2 = mysqli_real_escape_string($db, $_POST['password2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($fullName)) { array_push($errors, "Full Name is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($phoneNum)) { array_push($errors, "Phone Number is required"); }
  if (empty($password1)) { array_push($errors, "Password is required"); }
  if (empty($password2)) { array_push($errors, "Password repeat is required"); }
  if ($password1 != $password2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM admin WHERE fullname='$fullName' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO admin (fullName, email, phoneNum, password1, password2) 
              VALUES('$fullName', '$email', '$phoneNum', '$password1', 'password2')";
    mysqli_query($db, $query);
    $_SESSION['fullName'] = $fullName;
    $_SESSION['success'] = "You are now registered";
    header('location: index.php');
  }
}

register2.html

<!doctype html>
<html class="no-js" lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Sign up MDFF</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" type="image/png" href="assets/images/icon/fire2.png">
</head>

<body>
    <!-- preloader area start -->
    <div id="preloader">
        <div class="loader"></div>
    </div>
    <!-- preloader area end -->
    <!-- login area start -->
    <div class="login-area login-s2">
        <div class="container">
            <div class="login-box ptb--95">
                <form name="register2" action="db_reg.php" method="POST">
                    <div class="login-form-head">
                        <h3>MONITORING & DETECTION FOREST FIRE</h3>
                        <br></br>
                        <h4>Sign Up</h4>
                    </div>
                    <div class="login-form-body">
                        <div class="form-gp">
                            <label for="Inputfullname">Full Name</label>
                            <input type="text" id="Inputfullname" name="fullname" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputEmail1">Email address</label>
                            <input type="email" id="exampleInputEmail1" name="email" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputPhone1">Phone Number</label>
                            <input type="text" id="exampleInputPhone1" name="phoneNum" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputPassword1">Password</label>
                            <input type="password" id="exampleInputPassword1" name="password1" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputPassword2">Confirm Password</label>
                            <input type="password" id="exampleInputPassword2" name="password2" required>

                        </div>
                        <div class="submit-btn-area">
                            <a href="#" class="btn btn-primary btn-md" role="button">Submit   </a>                
                        </div>
                        <div class="form-footer text-center mt-5">
                            <p class="text-muted">Do have an account? <a href="login2.html">Sign in</a></p>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- login area end -->
</body>
</html>

3 个答案:

答案 0 :(得分:0)

您应该将操作添加到表单中,而不是将操作添加到div中:

<form action="db_reg.php" method="post">

答案 1 :(得分:0)

您的PHP脚本正在等待$_POST["register2"],该脚本永远不会到达。

在这里->确保您的提交按钮具有name="register2"属性,或者您可以在html <input type="hidden" name="register2"内的某个地方添加form

<div class="submit-btn-area">
    <a href="#" class="btn btn-primary btn-md" role="button">Submit</a> 
</div>

我不使用引导程序,但是我想上面的标记将变成一个提交按钮,如果是这种情况,您可以向其中添加name="register 2"属性如下

<div class="submit-btn-area">
    <a href="#" class="btn btn-primary btn-md" role="button" name="register2">Submit</a> 
</div>

EDIT 在PHP脚本的顶部,您可以通过调用$_POST来检查var_dump($_POST)变量中包含的内容,并查看表单数据是否存在。

如果不存在,则可能需要按如下所示将上面的标记更改为“提交”按钮

<div class="submit-btn-area">
    <input type="submit" name="register2" value="Submit" class="btn btn-primary btn-md" role="button"> 
</div>

答案 2 :(得分:0)

使用

<div class="submit-btn-area">

<input type="submit" name="register2">

</div>