index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="proccess.php" class="form" method="POST">
<label for="">name</label>
<input type="text" name="name" id="" >
<label for="">family</label>
<input type="text" name="family" id="" >
<label for="">phone</label>
<input type="text" name="phone" id="" >
<label for="">email</label>
<input type="text" name="email" id="" >
<input type="submit" value="Register">
</form>
</body>
</html>
style.css
.form {
display: flex;
flex-direction: column;
align-items: center;
}
[type="text"],
[type="submit"] {
margin-bottom: 1%;
padding: 9px 20px;
width: 200px;
}
functions
<?php
function connectDb()
{
$mysqliObj = new mysqli("localhost", "root", "", "oophp");
if ($mysqliObj->connect_errno) {
echo $mysqliObj->connect_error;
die();
}
return $mysqliObj;
}
function getAllData($mySqliObj, $tableName = "names")
{
return $mySqliObj->query("SELECT * FROM $tableName");
}
function validateData($data)
{
foreach ($data as $key => $value) {
if (empty($value)) {
return $key;
}
}
}
function emailAvilable($mySqliObj, $email){
$emails = $mySqliObj->query("SELECT email FROM names where email = '$email' ");
if ($emails->num_rows){
return true;
}
}
function insertInDb ($mySqliObj, $data) {
$prepareData = $mySqliObj->prepare("INSERT INTO names VALUES(?, ?,?,?)");
$prepareData->bind_param("ssss", $data['name'],$data['family'],$data['phone'],$data['email']);
$prepareData->execute();
return true;
}
proccess.php
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
header("location:http://127.0.0.1/miniproject");
} else if ($_SERVER['REQUEST_METHOD'] == "POST") {
require "functions.php";
if (validateData($_POST)){
echo "Fill all the textboxes";
die();
}else {
$data = array(
"name" => $_POST['name'],
"family" => $_POST['family'],
"phone" => $_POST['phone'],
"email" => $_POST['email']
);
}
$mysqliObj = connectDb();
if (emailAvilable($mysqliObj,$data['email'])) {
echo "Email is existed<br>";
echo " <a href='http://127.0.0.1/miniproject'>Return</a>";
die();
}
insertInDb($mysqliObj, $data);
}