sql – I can’t create a user object in php

I want to create an object user for a website project that I am using. I have made the blueprint of the class in a saperate file called calsses.php and it contains the following code:

<?php 
class User {
    private $username;
    private $email;
    private $address;
    private $password;
    private $telephone;

    public function __construct($username, $email, $address, $password, $telephone) {
        $this->$username = $username;
        $this->$email = $email;
        $this->$address = $address;
        $this->$password = $password;
        $this->$telephone = $telephone;
    }

    public function getUsername() {
        return $this->username;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getAddress() {
        return $this->address;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getTelephone() {
        return $this->telephone;
    }
}
?>

After I create the user object in the sign_up.php I try to output a value from it but nothing happens even though the $_POST['key'] values ​​exist normally. The code is the following:

<?php
include("../php/functions.php");
include("../php/connect.php");
include("../php/classes.php");

/****** This is not important for the problem I think ********/
if(isset($_POST['submit-btn'])) {
    $nameTaken = nameExists($conn, $_POST['username']);
    if($nameTaken = null) {
        echo "name null";/**TODO: Do something when it doesn't connect to a database*/
        return;
    } else if($nameTaken) {
        echo "name taken";/**TODO: Do somethging when the username is taken*/
        return;
    }
    

    $emailTaken = emailExists($conn, $_POST['email']);
    if($emailTaken = null) {
        echo "email null";/**TODO: Do something when it doesn't connect to a database*/
        return;
    } else if($nameTaken) {
        echo "email taken";/**TODO: Do somethging when the email is taken*/
        return;
    }

/*************************** ******************************/

    //echo $_POST["username"], $_POST["email"], $_POST["address"], $_POST["password"], $_POST["telephone"] . "<br>";
    $user = new User($_POST["username"], $_POST["email"], $_POST["address"], $_POST["password"], $_POST["telephone"]);

    echo $user->getEmail() . " | works";

    if(signUp($conn, $user)) {
        mysqli_close($conn);
        header('Location: ../html/homepage.html');
    }
}
// The html code of the page is the following if you need it :)
?>
<!DOCTYPE html>
<html lang="el">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/sign-up-style.css">
    <link rel="stylesheet" href="../css/general.css">
    <title>Composting Sign Up</title>
    <link rel="shortcut icon" href="../images/composting200.png">
</head>
<body>

<!------------ HEADER ------------->
<?php require("../php/header.php")?>

<!------------- MAIN CONTAINER -------------->
<div class="form-container container-height">
    <h1 class="top-text">Sign Up</h1>
    <form method="post" action="sign_up.php">
        <!-------- INPUT FIELDS --------->
        <div class="input-field">
            <input type="text" name="username" placeholder=" " id="sign-up-username" required>
            <span></span>
            <label for="sign-up-username">Όνομα Χρήστη</label>
        </div>

        <div class="input-field">
            <input type="email" name="email" placeholder=" " id="sign-up-email" required>
            <span></span>
            <label for="sign-up-email">Email</label>
        </div>

        <div class="input-field">
            <input type="text" name="address" placeholder=" " id="sign-up-address" required>
            <span></span>
            <label for="sign-up-address">Διεύθυνση</label>
        </div>

        <div class="input-field">
            <input type="password" name="password" placeholder=" " id="sign-up-password" required>
            <span></span>
            <label for="sign-up-password">Κωδικός</label>
        </div>

        <div class="input-field">
            <input type="tel" name="telephone" placeholder=" " id="sign-up-tel">
            <span></span>
            <label for="sign-up-tel">Τηλέφωνο</label>
        </div>
        <!--------------------------------->

        <!---------- SUBMIT BUTTON ----------->
        <div class="btn-space">
            <button name="submit-btn" type="submit" class="submit-btn">
                Sign Up
            </button>
        </div>
        <!--------------------------------->

        <!-- LOG IN LINK -->
        <div class="log-in-text">Έχεις Λογαριασμό? <a href="log_in.php">Log In!</a></div>
        <!----------------->

    </form>
</div>

<script type="text/javascript" src="../javascript/script.js"></script>

</body>
</html>

Ideally I want to create the object and pass it as an arguent in the signUp($conn, $user) function that I declared in the functions.php file which will add the user to the database and log him in the site through the $_SESSION['user'] = $user but for some reason when I try to output something from the object $user that I created, I eater get nothing back or I get () as an output. The problem starts in the sign_up.php file. The code for the functions.php file is the follwing:

<?php
session_start();
include("../php/connect.php");

function signUp($conn, $user) {
     return addAccountToDB($conn ,$user);
}

function addAccountToDB($conn, $user) {
    $sql_query = "INSERT INTO users(username, email, address, password, telephone) VALUES ('$user->getUsername()','$user->getEmail()','$user->getAddress()','$user->getPassword()','$user->getTelephone()')";
    if(mysqli_query($conn, $sql_query)) {
        logIn($conn, $user, false);
        return true;
    }
    echo "query error " . mysqli_error($conn);
    return false;
    
}

function logIn($conn, $user, $checkExistance) {
    if($checkExistance) {
        $sql_query = "SELECT email, password FROM users WHERE email="$user->email"";
        $result = mysqli_query($conn, $sql_query);
        // Haven't written all the code yet 
    }
    $_SESSION["user"] = $user;
}


?>

Also this is the code for the connection to the server but it works:

<?php

$user="*name*";
$pass="*password*";
$db = '*dbname*';

$conn = mysqli_connect('localhost', $user, $pass, $db) or die("Unable to connect");

?>

Leave a Comment