-1

I'm receiving the following error message when I submit user data from my form to the db

Fatal error: Uncaught Error: Call to undefined function insertUserDetails() in /Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/accounts/Account.php:175 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/handlers/register-handlers.php(49): Account->register('Bartthewonder', 'Bartholemewjohn...', 'Simpson', 'Bart.simpson@gm...', 'Bart.simpson@gm...', 'password', 'password') #1 /Applications/XAMPP/xamppfiles/htdocs/spotifyclone/register.php(21): include('/Applications/X...') #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/accounts/Account.php on line 175

insertUserDetails is the function in question .

Line 175 from my account.php file is the following:

return insertUserDetails($un, $fn, $ln, $em, $pw);

Below is the full code pasted

<?php
 class Account {

        private $con;
        private $errorArray;

        public function __construct($con) {
            $this->con = $con;
            $this->errorArray = array();
        }

        public function register($un, $fn, $ln, $em, $em2, $pw, $pw2) {
            $this->validateUsername($un);
            $this->validateFirstName($fn);
            $this->validateLastName($ln);
            $this->validateEmails($em, $em2);
            $this->validatePasswords($pw, $pw2);

            if(empty($this->errorArray) == true) {
                //Insert into db
                return insertUserDetails($un, $fn, $ln, $em, $pw);
            }
            else {
                return false;
            }

        }

        public function getError($error) {
            if(!in_array($error, $this->errorArray)) {
                $error = "";
            }
            return "<span class='errorMessage'>$error</span>";
        }

        private function insertUserDetails($un, $fn, $ln, $em, $pw) {
            $encryptedPw = md5($pw);
            $profilePic = "assets/images/profile-pics/head_emerald.png";
            $date = date("Y-m-d");

            $result = mysqli_query($this->con, "INSERT INTO users VALUES ('', '$un', '$fn', '$ln', '$em', '$encryptedPw', '$date', '$profilePic')");

            return $result;
        }

        private function validateUsername($un) {

            if(strlen($un) > 25 || strlen($un) < 5) {
                array_push($this->errorArray, Constants::$usernameCharacters);
                return;
            }

            //TODO: check if username exists

        }

        private function validateFirstName($fn) {
            if(strlen($fn) > 25 || strlen($fn) < 2) {
                array_push($this->errorArray, Constants::$firstNameCharacters);
                return;
            }
        }

        private function validateLastName($ln) {
            if(strlen($ln) > 25 || strlen($ln) < 2) {
                array_push($this->errorArray, Constants::$lastNameCharacters);
                return;
            }
        }

        private function validateEmails($em, $em2) {
            if($em != $em2) {
                array_push($this->errorArray, Constants::$emailsDoNotMatch);
                return;
            }

            if(!filter_var($em, FILTER_VALIDATE_EMAIL)) {
                array_push($this->errorArray, Constants::$emailInvalid);
                return;
            }

            //TODO: Check that username hasn't already been used

        }

        private function validatePasswords($pw, $pw2) {

            if($pw != $pw2) {
                array_push($this->errorArray, Constants::$passwordsDoNoMatch);
                return;
            }

            if(preg_match('/[^A-Za-z0-9]/', $pw)) {
                array_push($this->errorArray, Constants::$passwordNotAlphanumeric);
                return;
            }

            if(strlen($pw) > 30 || strlen($pw) < 5) {
                array_push($this->errorArray, Constants::$passwordCharacters);
                return;
            }
        }
    }
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    In the future please take the time to format your questions content. Your implementing some very bad practices with this code. MD5 hashing (note not an encryption as variable name implies), values/variables in SQL, passwords only allowed to contain alphanumerical characters. Your `validateUsername` is the same as `insertUserDetails` not sure why you are calling them differently. – user3783243 Apr 22 '20 at 05:31
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Apr 22 '20 at 19:29
  • `md5` is not an encryption method! It is a hash function. – Dharman Apr 22 '20 at 19:29
  • Thank you Dharman and user3783243! appreciate the guidance – Hans Alejandro May 17 '20 at 17:33

1 Answers1

1

insertUserDetails is not a regular function, it is a class method. It uses internal properties of the object (here, the connection $this->con) and it needs to be applied on an object to be executed.

$this refers to the current object, so it is indeed the solution in your case.

This is a basic concept in Object-oriented programming, so take a look at the first chapters at least if you want to work with these concepts : PHP Classes and Objects

Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28