0

Hey guys so currently I am teaching myself PHP but have come across a very confusing issue. I am getting the following errors when running another file that includes this code in a separate file.

Warning: Undefined array key "first_name" in C:\xampp\htdocs\MirrorTrade\classes\signup.php on line 26 Warning: Undefined array key "last_name" in C:\xampp\htdocs\MirrorTrade\classes\signup.php on line 27 Warning: Undefined array key "email" in C:\xampp\htdocs\MirrorTrade\classes\signup.php on line 28 Warning: Undefined array key "password" in C:\xampp\htdocs\MirrorTrade\classes\signup.php on line 29 Fatal error: Uncaught Error: Call to undefined function create_userid() in C:\xampp\htdocs\MirrorTrade\classes\signup.php:32 Stack trace: #0 C:\xampp\htdocs\MirrorTrade\classes\signup.php(17): Signup->create_user(Array) #1 C:\xampp\htdocs\MirrorTrade\signup.php(7): Signup->evaluate(Array) #2 {main} thrown in C:\xampp\htdocs\MirrorTrade\classes\signup.php on line 32

And here is my code that is having issues

<?php

class Signup
{
private $error = "";
public function evaluate($data)
    {
        foreach ($data as $key => $value) 
            {
            if(empty($value))
            {
                $this->error = $this->error . $key . " is empty!<br>";
            }
        }
        if($this->error == "")
        {
            $this->create_user($data);
        }
        else
        {
            return $this->error;
        }
    }
public function create_user($data)
    {
        $firstname = $data['first_name'];
        $lastname = $data['last_name'];
        $email = $data['email'];
        $password = $data['password'];

        $url_address = strtolower($firstname) . "." . strtolower($lastname);
        $userid = create_userid();

        $query = "insert into 
        users
        (userid,first_name,last_name,email,password,url_address)
        values
        ('$userid','$first_name','$last_name','$email','$password','$url_address')";
        return $query;
    }
    
private function create_userid()
    {
        $length = rand(4,19);
        $number = "";
        for ($i=0; $i < $length; $i++) 
        { 
            $new_rand = rand(0,9);
            $number = $number . $new_rand;
        }
        return $number;
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Dildan
  • 25
  • 4

1 Answers1

1

When you refer to another method inside the same class, you need to prefix it with $this. So this code:

$userid = create_userid();

Should be:

$userid = $this->create_userid();

Also note, your code is vulnerable to SQL injection attacks. Instead of building queries with string concatenation, always use prepared statements with bound parameters. See this page and this post for some good examples.

And never store plain text passwords. Instead use password_hash() and password_verify().

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Thank you for the solution! I understand my code is not completely safe and is vulnerable but this is covered later in the videos I am following :) Thank you! – Dildan Apr 07 '21 at 22:31