<?php
echo 'connecting..';
$servername = "localhost";
$username = "root";
$password = "";
$connname = "eng";

// Create connection
$conn = new mysqli($servername, $username, $password, $connname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}else{
    echo 'Connected';
}
 
    $user_name = $_POST['name'];
    $user_email = $_POST['email'];
    $user_pass = $_POST['password'];
    echo 'submitting..';    
    echo 'User Name is: ' .$user_name;
    echo 'Email: ' .$user_email;

$sql = "INSERT INTO users (user_name, user_pass, user_email, user_role) VALUES ('".$user_name."','".$user_pass."','".$user_email."', 2)";

//$statusMsg = '';

if ($conn->query($sql) === TRUE) {
    ?>
 <script>
    var Toast = Swal.mixin({
      toast: true,
      position: 'top-end',
      showConfirmButton: false,
      timer: 3000
    });
    Toast.fire({
    icon: 'success',
    title: 'New User Successfuly Added.'
      });
</script>
  <?php
} else {
    ?>
    <script>
            var Toast = Swal.mixin({
            toast: true,
            position: 'top-end',
            showConfirmButton: false,
            timer: 3000
            });
            Toast.fire({
            icon: 'error',
            title: '<?php echo "Error: " . $sql . "<br>" . $conn->error; ?>'
            });
    </script>
  <?php
}

$conn->close();
/*
<!--
include '../dbConfig.php'; //Connection
    $statusMsg = '';
    ini_set('display_errors', 1);
    ini_set('display_start_up_errors', 1);
    error_reporting(E_ALL);
    if(isset($_POST['submit'])) {    
    $user_name = $_POST['name'];
    $user_email = $_POST['email'];
    $user_pass = $_POST['password'];
        
// checking empty fields
if(empty($user_name) || empty($user_email) || empty($user_pass)) {                
    if(empty($user_pass)) {
        $user_pass = 'User123#';
    }
} else {    
    $insert = $conn->query("INSERT INTO users(user_name, user_pass, user_email, user_role) VALUES ('".$user_name."','".$user_pass."','".$user_email."', 2)");
    if($insert){
        $statusMsg = "User ".$user_name." has been added successfully.";
    }else{
        $statusMsg = "Connection failed, please try again.";
    } 
    //$addnewaction = "INSERT INTO users(user_name, user_pass, user_email, user_role) VALUES ('$user_name','$user_pass','$user_email', 2)";
    //$result = mysqli_query($conn, $addnewaction);
    //display success message
    //echo "<font color='green'>Data added successfully. Page will refresh in 30 Seconds.</strong>";
    //header("Refresh:30");
    }
} /*elseif(isset($_POST['Update'])) {
    $plate = $_GET['value'];

    $sql2 = "UPDATE actions SET time_out = CURTIME, price = 250, status = 1 WHERE plate = '$plate' AND status = 0";
    $result2 = mysqli_query($conn, $sql2);
    echo "<font color='green'>Car is out of parking. Page will refresh in 30 Seconds.</strong>";
    header("Refresh:30");
}*/
//echo $statusMsg; 
?> 