<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST["username"];
    $email = $_POST["email"];
    $password = $_POST["password"];

    $servername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbname = "transaction";

    $NAME_1 = new mysqli($servername, $dbUsername, $dbPassword, $dbname);
    if ($NAME_1->connect_error) {
        die("Connection failed: " . $NAME_1->connect_error);
    }

    function emptyInputSubmit($username, $email, $password) {
        return empty($username) || empty($email) || empty($password);
    }

    function invalidUsername($username) {
        return !preg_match("/^[a-zA-Z0-9]*$/", $username);
    }

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $username = $_POST["username"];
        $email = $_POST["email"];
        $password = $_POST["password"];

        $servername = "localhost";
        $dbUsername = "root";
        $dbPassword = "";
        $dbname = "transaction";

        $NAME_1 = new mysqli($servername, $dbUsername, $dbPassword, $dbname);
        if ($NAME_1->connect_error) {
            die("Connection failed: " . $NAME_1->connect_error);
        }

        function emptyInputSubmit($username, $email, $password)
        {
            return empty($username) || empty($email) || empty($password);
        }

        function invalidUsername($username)
        {
            return !preg_match("/^[a-zA-Z0-9]*$/", $username);
        }

        function invalidEmail($email)
        {
            return !filter_var($email, FILTER_VALIDATE_EMAIL);
        }

        function createUser($NAME_1, $username, $email, $password)
        {
            $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
            $sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
            $stmt = $NAME_1->prepare($sql);
            $stmt->bind_param("sss", $username, $email, $hashedPassword);
            $stmt->execute();
            $stmt->close();
        }

        function getUserByEmail($NAME_1, $email)
        {
            $sql = "SELECT * FROM users WHERE email = ?";
            $stmt = $NAME_1->prepare($sql);
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $result = $stmt->get_result();
            $user = $result->fetch_assoc();
            $stmt->close();
            return $user;
        