WANT TO ADD A preg_match to this form on the Course field for user input validation before it enters the database. The Condition I want is: ('/^[A-Z]{3}[0-9]{3}/') Kindly add the preg_match condition on course field for me. Thank you. THE CODE:
I WANT TO ADD A preg_match to this form on the Course field for user input validation before it enters the
The Condition I want is: ('/^[A-Z]{3}[0-9]{3}/')
Kindly add the preg_match condition on course field for me.
Thank you.
THE CODE:
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div class ="formdiv">
<h2> FORM VALIDATE</h2>
<form action="" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="fname"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>Qualification</td>
<td><input type="text" name="qual"></td>
</tr>
<tr>
<td>Course</td>
<td><input type="text" name="course"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) &&
isset($_POST['age']) && isset($_POST['qual'])&& isset($_POST['course'])){
echo '<div style="width: 400px;text-align: center;margin: 210px auto;border: 1px solid black;color:#606060FF;background-color: #D6ED17FF;">';
// get the form input values and store in variables
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$age = $_POST['age'];
$qualifiacation = $_POST['qual'];
$course = $_POST['course'];
// if input boxes are blank, error message is shown
if($firstname == "" || $lastname == "" || $email == "" ||
$age == "" || $qualifiacation == "" || $course == ""){
echo '<h2> Please input all fields </h2>';
}
else{
$servername = "localhost";
$username = "root";
$password = "password";
$databasename = "user_data";
// Create connection
$conn = new mysqli($servername, $username, $password, $databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user_details (first_name, last_name, email, age, qualification, Course)
VALUES ('$firstname', '$lastname', '$email', '$age', '$qualifiacation', '$course')";
if ($conn->query($sql) === TRUE) {
echo "<h1>Hello ". $firstname." ". $lastname ."</h1>";
echo "<h2 style =color:#606060FF;background-color:#D6ED17FF;>Form data has been sent to database successfully.</h2>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
}
?>
</div>
</body>
</html>
Step by step
Solved in 3 steps with 2 images