gpt4 book ai didi

php - 无法抛出错误消息

转载 作者:行者123 更新时间:2023-11-30 21:44:50 25 4
gpt4 key购买 nike

登记表

 <!DOCTYPE HTML>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="" method="POST">
Name:
<input type="text" name="name">
<br/> <br/>
Username:
<input type="text" name="username">
<br/> <br/>
Password:
<input type="password" name="password">
<br/> <br/>

Email:
<input type="text" name="email">
<br/> <br/>
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>
<?php
require('connect.php');
require('validation.php');
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];

$email = $_POST['email'];

if(isset($_POST["submit"])){
if($query = mysqli_query($connect,"INSERT INTO users
(`id`,`name`,`username`, `password`, `email`) VALUES ('','".$name."',
'".$username."', '".$password."', '".$email."')")){
echo "Success";
}else{
echo "Failure" . mysqli_error($connect);
}
}
?>

验证.php

     <?php
// define variables and set to empty values
$nameErr = $emailErr = $userErr = $passwordErr = "";
$name = $email = $username =$password = "";


if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

if (empty($_POST["username"])) {
$userErr = "Username is required";
} else {
$username = test_input($_POST["username"]);
}

if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password= test_input($_POST["password"]);
}

}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

连接.php

    <?php
$connect = mysqli_connect("localhost", "root", "","php_forum")
or die("Error " . mysqli_error($connect));
?>

我正在开发一个简单的注册,有四个输入,即姓名、用户名、密码、电子邮件。当用户填写表格并单击提交按钮时,所有填写的数据都应该保存在正在运行的数据库中在我的情况下很好,但是当用户不填写任何数据并且如果用户只是点击提交按钮那么错误消息应该显示为“所有字段都是必要的”,但在我的情况下即使我点击提交按钮没有输入任何值我得到的消息是成功的,所有空值都存储在数据库中,这不应该发生,我的输出应该是如果我填写表格 n 单击提交按钮,那么所有数据都应该存储在数据库中,如果我单击提交按钮而不填写任何值然后错误应该抛出“所有要填写的字段”并且数据库中不应存储空值,请任何人指导我应该做哪些更改以获得我想要的输出。

最佳答案

你会

require('validation.php');

设置和检查很多变量,例如

 $name = test_input($_POST["name"]);
$nameErr = "Only letters and white space allowed";

从 require 返回并用原始发布的值覆盖 $name 并且不对 $nameErr 做任何事情

 $name = $_POST['name'];

检查require之后的错误

例如

require('validation.php');
$lsError = $nameErr . $emailErr . $userErr . $passwordErr;
if(trim($lsError) != '') {
echo $lsError ."<BR>";
echo "ALL FIELDS ARE NECESSARY";
}
else {
//rest of your insert
}

关于php - 无法抛出错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49935483/

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