我正在使用html5、php和sql进行库管理项目。目前,我正在使用php进行表单验证。验证应用于电子邮件、密码、姓名、姓氏和电话号码。验证有效(至少在视觉上有效(,但它在数据库中使用空条目。那么如何解决这个问题呢?
Html代码
<?php
require '..\modelsignup.php';
require '..\modeladu.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<style media="screen">
main{
margin-left:300px;
margin-top:70px;
}
</style>
<title>Create Librarian</title>
</head>
<body>
<?php include '..\templatesdashboard.php'; ?>
<main>
<h3 class="my-4">Create new librarian</h3>
<p><span class="error">* required field</span></p>
<form action="createlibrarian.php" method="post" name="">
<div class="form-group" >
<label class="mt-2" for="name">Name</label>
<input id="name" class="form-control" type="text" name="name" value="<?php echo $fname;?>">
<span class="error" style="color:red">* <?php echo $fnameErr;?></span>
<br><br>
<label class="mt-2" for="surname">Surname</label>
<input class="form-control" type="text" name="surname" value="<?php echo $fname;?>">
<span class="error" style="color:red">* <?php echo $lnameErr;?></span>
<br><br>
<label class="mt-2" for="email">Email</label>
<input class="form-control" type="text" name="email" value="<?php echo $email;?>">
<span class="error" style="color:red">* <?php echo $emailErr;?></span>
<br><br>
<label class="mt-2" for="password">Password</label>
<input class="form-control" type="password" name="password" value="<?php echo $pass;?>">
<span class="error" style="color:red">* <?php echo $passErr;?></span>
<br><br>
<label class="mt-2" for="phone">Phone</label>
<input class="form-control" type="text" name="phone" value="<?php echo $phone;?>">
<span class="error" style="color:red">* <?php echo $phoneErr;?></span>
<br><br>
<input type="submit" name="submit" value="Create" class="mt-2 btn btn-primary">
</div>
</form>
</main>
</body>
</html>
php代码
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// define variables and set to empty values
$fnameErr = $lnameErr= $emailErr = $passErr = $phoneErr = "";
$fname = $email = $lname = $pass = $phone = "";
$flag;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$fnameErr = "First Name is required";
$flag=false;
} else {
$fname = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameErr = "Only letters and white space allowed";
return false;
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$flag= false;
} 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";
return false;
}
}
if (empty($_POST["surname"])) {
$lnameErr = "Surname is required";
$flag= false;
} else {
$lname = test_input($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameErr = "Only letters and white space allowed";
return false;
}
}
if (empty($_POST["password"])) {
$passErr = "Password is required";
$flag= false;
} else {
$pass = test_input($_POST["password"]);
// check if name only contains letters and whitespace
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^w]@', $password);
if(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) {
echo 'Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.';
return false;
}
}
if (empty($_POST["phone"])) {
$phoneErr = "phone number is required";
$flag= false;
} else {
$phone = test_input($_POST["phone"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9]{5}-[0-9]{5}$/",$phone)) {
$phoneErr = "phone number should be numeric ";
return false;
}
}
return $flag;
echo "<h2>Your Input:</h2>";
echo $fname;
echo "<br>";
echo $lname;
echo "<br>";
echo $email;
echo "<br>";
echo $pass;
echo "<br>";
echo $phone;
echo "<br>";
}
?>
如何解决这个问题?
您的验证看起来是正确的。我会在yout标签中添加一个"必需"属性:
<input class="form-control" type="text" name="surname" value="<?php echo $fname;?>" required>
还要确保SQL列中有一个NOT NULL约束。您可以通过以下方式修改现有列:
ALTER TABLE Person MODIFY name VARCHAR(255) NOT NULL;