为啥html页面不读取php页面
Posted
技术标签:
【中文标题】为啥html页面不读取php页面【英文标题】:Why html page does not read the php page为什么html页面不读取php页面 【发布时间】:2020-02-09 16:09:30 【问题描述】:我点击提交按钮时遇到问题,没有发生任何事情,并且我的数据没有被插入到数据库中。
我正在尝试制作一个注册页面(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>
【问题讨论】:
切勿以明文形式或使用 MD5/SHA1 存储密码! 仅存储使用 PHP 的password_hash()
创建的密码哈希,然后您可以使用 password_verify()
进行验证。看看这个帖子:How to use password_hash 并了解更多关于bcrypt & password hashing in PHP
How to get the error message in MySQLi?
感谢您的评论。我会记住这一点
不清楚你在问什么。 HTML 页面永远不会“读取”PHP,但 PHP 页面肯定会被表单操作调用,就像您拥有它一样。我没有看到您的表单的提交按钮,所以这可能是您的问题。您拥有的提交是一个链接而不是表单提交按钮。
注意,在初始化变量时,建议将它们设置为空字符串以避免未定义的值,以防不存在 $_POST
全局变量,即未通过 POST 访问脚本
【参考方案1】:
您应该向表单添加操作,而不是向 div 添加操作:
<form action="db_reg.php" method="post">
【讨论】:
【参考方案2】:您的 PHP 脚本正在等待 $_POST["register2"]
,但它永远不会到达。
在这里 -> 确保您的提交按钮具有 name="register2"
属性,或者您可以在 html form
中的某处添加 <input type="hidden" name="register2"
<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>
编辑
在 PHP 脚本的顶部,您可以通过调用 var_dump($_POST)
检查您的$_POST
变量包含的内容,并查看您的表单数据是否存在。
如果它们不存在,您可能需要将上面的标记更改为提交按钮,如下所示
<div class="submit-btn-area">
<input type="submit" name="register2" value="Submit" class="btn btn-primary btn-md" role="button">
</div>
【讨论】:
【参考方案3】:使用
<div class="submit-btn-area">
<input type="submit" name="register2">
</div>
【讨论】:
以上是关于为啥html页面不读取php页面的主要内容,如果未能解决你的问题,请参考以下文章
HTML iframe 标签 在手机 打开PDF 格式文件 为啥会加载不出来 显示空白页面
thinkphp把HTML+PHP写的一个页面代码存入数据库以后 读取出来的效果HTML代码解析了PHP代码被直接输出
thinkphp把HTML+PHP写的一个页面代码存入数据库以后 读取出来的效果HTML代码解析了PHP代码被直接输出