<?php
	include 'includes/session.php';
	include 'includes/sessions.php'; 

	if(isset($_POST['submit'])){
	    $length = 15;
	    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         $charactersLength = strlen($characters);
         $randomPassword = '';
    
    for ($i = 0; $i < $length; $i++) {
        $randomPassword .= $characters[rand(0, $charactersLength - 1)];
    }
    
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		$name=$_POST['name'];
		$phone=$_POST['phone'];
		$email=$_POST['email'];
		$address=$_POST['address'];
		$password=$randomPassword;

		$sql = "INSERT INTO users (first_name, last_name,name,phone,email,address,password) VALUES ('$first_name', '$last_name','$name','$phone','$email','$address','$password')";
		if($conn->query($sql)){

			$_SESSION['success'] = 'User added successfully';
		}
		else{ 
			$_SESSION['error'] = $conn->error;
		}
	}	
	else{
		$_SESSION['error'] = 'Fill up add form first';
	}

	header('location: users.php');

?>