gpt4 book ai didi

php - 为什么html页面读不到php页面

转载 作者:行者123 更新时间:2023-11-29 15:34:52 25 4
gpt4 key购买 nike

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

我正在尝试制作一个注册页面(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>

最佳答案

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

此处 -> 确保您的提交按钮有 name="register2"属性或者您可以添加 <input type="hidden" name="register2"你的 html form 里面的某个地方

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

我不使用 bootstrap,但我猜上面的标记会变成提交按钮,如果是这种情况,您可以添加 name="register 2"属性如下

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

编辑在 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>

关于php - 为什么html页面读不到php页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com