0

While working with a project on Xampp with php 7.4 getting this error. The line 553 is;

if ($this->data['avatar_id'] == 0)

I realize that it is searching avatar_id in DB_USERS but it is located in DB_ACCOUNTS. I think this is caused the problem but i couldn't find how to solve.

These are to complete code block;

    private function getData()
{
    global $config;
    $query = $this->getConnection()->query("SELECT * FROM " . DB_USERS . " WHERE id=" . $this->id);
    
    if ($query->num_rows == 1)
    {
        $fetch = $query->fetch_array(MYSQLI_ASSOC);
        
        if (! empty($fetch['birthday']))
        {
            if (!preg_match('/(\-|\/)/', $fetch['birthday'])) $this->getConnection()->query("UPDATE " . DB_USERS . " SET birthday='01/01/2000' WHERE id=" . $this->id);

            $fetch['birth'] = explode('-', $fetch['birthday']);
            if (isset($fetch['birth'][1]))
            {
                $fetch['birth'] = array(
                    'date' => (int) $fetch['birth'][0],
                    'month' => (int) $fetch['birth'][1],
                    'year' => (int) $fetch['birth'][2]
                );
            }
            else
            {
                $fetch['birth'] = explode('/', $fetch['birthday']);
                $fetch['birth'] = array(
                    'date' => (int) $fetch['birth'][1],
                    'month' => (int) $fetch['birth'][0],
                    'year' => (int) $fetch['birth'][2]
                );
            }
        }
        
        if ($this->data['avatar_id'] == 0)
        {
            $fetch['thumbnail_url'] = $fetch['avatar_url'] = SITE_URL . '/' . $config['user_default_male_avatar'];
            
            if (! empty($fetch['gender']))
            {
                if ($fetch['gender'] == "female")
                {
                    $fetch['thumbnail_url'] = $fetch['avatar_url'] = SITE_URL . '/' . $config['user_default_female_avatar'];
                }
            }
        }

        $fetch['location'] = $fetch['current_city'];

        return $fetch;
    }
}
E. Given
  • 31
  • 4
  • 1
    Do you mean `$fetch` instead of `$this->data` for this line? I don't see anywhere else that you're using `$this->data`, and it sounds like it's either not filled or possibly not defined. – aynber Apr 30 '21 at 14:59
  • When i turned it to $fetch, error becoming; `Notice: Undefined index: avatar_id in C:\xampp7\htdocs\social\classes\User.class.php on line 553` – E. Given Apr 30 '21 at 15:25
  • 1
    Is `$fetch` supposed to have such index? – Dharman Apr 30 '21 at 15:31
  • The error is self-explanatory. `$this->data` is null and you're trying to treat it like an array. Ensure you're giving it a value, or check for a value, before attempting to access it. – miken32 Apr 30 '21 at 16:08
  • 1
    Also, you should use prepared statements and parameterized queries for all database access rather than stuffing variables into queries. Your current code is unsafe for use. – miken32 Apr 30 '21 at 16:10
  • And finally, don't use `global` variables. It's very poor practice, especially in an object-oriented setting where class properties are available. – miken32 Apr 30 '21 at 16:11

0 Answers0