In: Computer Science
Using PHP and MySQL
New submissions should appear in table
Create DataBase:
Create DATABASE myDB
// Create table
CREATE TABLE users(name varchar(30) not null , email varchar(50) not null,
phone int(10) not null, birthday varchar(25) , gender varchar(1) not null);
Form.php:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $birthday = $phone = "";
$updateDB=false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$updateDB=true;
if (empty($_POST["name"])) {
$nameErr = "Name is required";
$updateDB=false;
} 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";
$updateDB=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";
}
}
$phone = test_input($_POST["phone"]);
$birthday = test_input($_POST["birthday"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
table.center {
margin-left:auto;
margin-right:auto;
}
</style>
<h2>PHP Form Validation </h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="email" name="email">
<br><br>
phone: <input type="tel" name="phone">
<br><br>
birthday: <input name="birthday" type='date'></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="F">Female
<input type="radio" name="gender" value="M">Male
<input type="radio" name="gender" value="O">Other
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<p><h3>List of users</h3></p>
<table style="width:100%; margin-left: auto;
margin-right: auto; ">
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Birthday</th>
<th>Gender</th>
</tr>
<?php
//Database Connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if($updateDB){
$sql = "INSERT INTO users (name, email, phone, birthday, gender)
VALUES ('$name', '$email', $phone,'$birthday', '$gender' )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
//echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>";
echo('
<tr>
<td>'. $row["name"].'</td>
<td>'. $row["email"].'</td>
<td>'. $row["phone"].'</td>
<td>'. $row["birthday"].'</td>
<td>'. $row["gender"].'</td>
</tr>
');
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</body>
</html>