<?php
include_once('inc/connection.php');
include_once('inc/funtions.php') ;
session_start();
?>
<?php
$errors = array();
$first_name = '';
$last_name = '';
$email = '';
$password = '';
if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$req_fields =array('first_name',
'last_name', 'email', 'password');
foreach ($req_fields as $field) {
# code...
if (empty(trim($_POST[$field]))) {
$errors[] = $field. ' is required';
# code...
}
}
/*}
//if not one by one
if (empty(trim($_POST['first_name']))) {
$errors[] = 'first name is required';
# code...
}
if (empty(trim($_POST['last_name']))) {
$errors[] = 'last name is required';
# code...
}
if (empty(trim($_POST['email']))) {
$errors[] = 'email is required';
# code...
}
if (empty(trim($_POST['password']))) {
$errors[] = 'password is required';
# code...
}
# code...
}
*/
// checking max lenth
$max_len_fi =array('first_name' => 100, 'last_name' =>100, 'email' => 100, 'password' => 100);
foreach ($max_len_fi as $field => $max_len) {
# code...
if (strlen(trim($_POST[$field])) > $max_len) {
$errors[] = $field. ' must be less than ' . $max_len . ' charactors';
# code...
}
//checking email address
if(!is_email($_POST['email'])) {
$errors[] = 'email address is invaild';
}
}
//checking email address already exit
$email= mysqli_real_escape_string($connection, $_POST['email']);
$query= "SELECT * FROM user WHERE email = '{$email}' LIMIT 1";
$result_set = mysqli_query($connection, $query);
// if email address already exit/ store error
if ($result_set) {
if (mysqli_num_rows($result_set) ==1) {
$errors[]= 'email address already exit';
# code...
}
# code...
}
if (empty($errors)) {
// on erros found adding new rocrd
$first_name= mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name= mysqli_real_escape_string($connection, $_POST['last_name']);
$password= mysqli_real_escape_string($connection, $_POST['password']);
$query = "INSERT INTO user (first_name, last_name, email, password, is_deleted )
VALUES ('$first_name' , '$last_name', '$email' , '$password' ,0 )";
if ($connection->query($query) === TRUE) {
header('location: users.php');
}
else {
echo "Error: " . $query . "<br>" . $connection->error;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Users</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<div class="app-name">
<h2> ss company </h2>
</div>
<div class="name">
well come | <a href="logout.php"> logout </a>
</div>
</header>
<div class= "os">
<form action ="addnew-users.php" method="post">
<div class="details">
<h1> User list <span> <a href="add-new.php"> +Add new </a></span> </h1>
</div>
<?php if (!empty($errors)) {
echo 'this is errors' . '<br>';
foreach ($errors as $error) {
echo $error . '<br>';
# code...
}
# code...
} ?>
<p>
<label> First name </label>
<input type="text" name="first_name" <?php echo 'value="' .$first_name . '"';?> >
</p>
<p>
<label> Last name </label>
<input type="text" name="last_name" <?php echo 'value="' .$last_name . '"';?>>
</p>
<p>
<label> email </label>
<input type="text" name="email" <?php echo 'value="' .$email . '"';?>>
</p>
<p>
<label> password </label>
<input type="text" name="password">
</p>
<p>
<button type="submit" name="submit"> Sabmit </button>
</p>
</form>
</div>
</body>
</html>
Comments
Post a Comment