从.php注册
<!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));
?>
我正在开发一个简单的注册,有四个输入,即姓名、用户名、密码、电子邮件。当用户填写表单并单击提交按钮时,所有填写的数据都应保存在数据库中,在我的情况下,数据库运行良好,但是,当用户不填写任何数据时,如果用户只需单击提交按钮,则错误消息应显示为“所有字段都是必需的”,但在我的情况下,即使我单击提交按钮而不输入任何值,我得到的消息也是成功的,所有空值都存储在数据库中,这是不应该发生的,我的输出应该是,如果我填写表格,然后单击“提交”按钮,那么所有的数据都应该存储在数据库中,如果我单击“提交”按钮而没有填写任何值,那么错误应该抛出“要填写的所有字段”,并且数据库中不应该存储空值,请任何人都可以指导我应该做什么样的改变,以便得到我想要的输出。
3条答案
按热度按时间sauutmhj1#
r3i60tvu2#
如果您不介意再添加一点代码,您的代码如下:
在您的注册表中\u form.php
在connect.php中
vq8itlhq3#
请在会话中添加错误并在表单文件中打印会话。
在validation.php中
从.php注册