Jump to content

PHP function error Notice: Undefined index:


NaderH

Recommended Posts

Shortly I need to block a group of users at once according to their role status (banned or not) instead of blocking users one by one.
I tried using fk on update cascade to control the role status by making a fk user_role_status column in the users table refers to role_status column in the roles table but without luck because when I change a role status in roles table it changes the whole user_role_status column in users table not only the role I changed then it blocks all users not only the group I want to block because role_status in roles table is not unique and I can't make it unique because its all cells contain value='1', so I deleted the column and gave up that idea
 

// role_status in roles table is a fk refers to id in statuses table.
 
    statuses_table
    
    id       is_active              
    ------------------
    0          no                        
    1          ok                        
    
    roles_table
    
    id       role              role_status
    --------------------------------------
    1        Admins                1
    2        Editors               1
    3        Users                 1
    
    users_table
    
    id       user_name       user_level       
    ---------------------------------------
    1        Admin               1                                 
    2        Editor1             2                                  
    3        User1               3                                  
    4        Editor2             2                                 
    5        User2               3                                   
    6        User3               3  


             
 

and this is the function I use

 

--------------------------------------------------------------
Find role status
--------------------------------------------------------------

function find_by_current_rolStatus($role_status)
{
global $db;
$sql = "SELECT users.id FROM users JOIN roles ON roles.id = users.user_level WHERE roles.role_status = '{$db->escape($role_status)}'";
$result = $db->query($sql);
return($db->num_rows($result) === 0 ? true : false);
}
    
--------------------------------------------------------------
Function for checking if user role status banned or allowed
--------------------------------------------------------------
    
function login_require_roleStatus($require_role_status)
{
global $session;
$current_user = current_user();
$current_user_role_status = find_by_current_rolStatus($current_user['role_status']);//line 155
//if Role status Deactive
if ($current_user['role_status'] === '0')://line 157
$session->msg('Banned');
redirect('home.php',false);
//if user role allowed
elseif($current_user['role_status'] === '1')://line 161
return true;
endif;
}

 

and I got this error

Quote

Notice: Undefined index: role_status in sql.php on line 155
Notice: Undefined index: role_status in sql.php on line 157
Notice: Undefined index: role_status in sql.php on line 161

 I am trying to get the users whose user_level = role_id which its status = '0' or '1', I checked this SQL query with phpmyadmin and it is working fine, and actually I don't know what to do after that :( , I mean I don't know what to write with $current_user['role_status'] instead of role_status to get what I want.

Link to comment
Share on other sites

24 minutes ago, ginerjm said:

You are trying to reference an array called $current_user and the elements/indices do not exist in it.  Show us the current_user() function since that seems to be how $current_user is created.

  /*--------------------------------------------------------------*/
  /* Find current log in user by session id
  /*--------------------------------------------------------------*/
  function current_user(){
      static $current_user;
      global $db;
      if(!$current_user){
         if(isset($_SESSION['user_id'])):
             $user_id = intval($_SESSION['user_id']);
             $current_user = find_by_id('users',$user_id);
        endif;
      }
    return $current_user;
  }

 

Link to comment
Share on other sites

Add this to the current_user function:

echo "<pre>",print_r($current_user,true),"</pre>";

Put it just before you execute the return from there.  Let's see what is being put into $current_user

AND show us the new function after you do this as well as the output that gets generated from the echo.

Edited by ginerjm
Link to comment
Share on other sites

33 minutes ago, ginerjm said:

Add this to the current_user function:

echo "<pre>",print_r($current_user,true),"</pre>";

Put it just before you execute the return from there.  Let's see what is being put into $current_user

AND show us the new function after you do this as well as the output that gets generated from the echo.

  function current_user(){
      static $current_user;
      global $db;
      if(!$current_user){
         if(isset($_SESSION['user_id'])):
             $user_id = intval($_SESSION['user_id']);
             $current_user = find_by_id('users',$user_id);
        endif;
      }
		echo "<pre>",print_r($current_user,true),"</pre>";
    return $current_user;
  }

It printed all user data

Array
(
    [id] => 
    [name] => 
    [username] => 
    [password] => 
    [user_level] => 
    [image] => 
    [status] => 
    [last_login] => 
)

 

Edited by NaderH
Link to comment
Share on other sites

5 minutes ago, NaderH said:
  function current_user(){
      static $current_user;
      global $db;
      if(!$current_user){
         if(isset($_SESSION['user_id'])):
             $user_id = intval($_SESSION['user_id']);
             $current_user = find_by_id('users',$user_id);
        endif;
      }
		echo "<pre>",print_r($current_user,true),"</pre>";
    return $current_user;
  }

It printed all user data

Array
(
    [id] => 
    [name] => 
    [username] => 
    [password] => 
    [user_level] => 
    [image] => 
    [status] => 
    [last_login] => 
)

 

It prints that data 4 times per page.

Link to comment
Share on other sites

2 minutes ago, Barand said:

Now you know why you get an error when you try using $current_user['role_status']

I know that before ( there are not a column called like that in users table, I wrote it to ask what should I write instead of it, I needed help to complete the function.

Link to comment
Share on other sites

15 minutes ago, ginerjm said:

As Barand has said - the index you tried to use does not exist in the array.  Why are you using it?

I am not using it, I just put it here in that post to ask what should I write instead of it to complete the post, I could write my name to make it more clear :) , it was just an example, I am asking for help to complete the function ...

Link to comment
Share on other sites

Your $current_user array does not have an index 'role_status' and that is causing the error. It does have an index 'status' which may be the one you should be using. You should verify this by looking at your find_by_id() function to see what it is putting into the array.

Link to comment
Share on other sites

18 minutes ago, Barand said:

Your $current_user array does not have an index 'role_status' and that is causing the error. It does have an index 'status' which may be the one you should be using. You should verify this by looking at your find_by_id() function to see what it is putting into the array.

is there any way to block a group of users according to their role, I mean I want to block all editors instead of doing that one by one, status column in users table only block a user, but I want to block a group of users at once, the SQL query I wrote is already get the users with the blocked role, but I can't use that query without the function which will process what I need, if their any way to write a function that combines users and roles to get the users with the blocked roles and block them at once ?

Edited by NaderH
Link to comment
Share on other sites

19 minutes ago, Barand said:

If I understand your data, set the editor role status to 0 to block all editors.

roles_table
    
    id       role              role_status
    --------------------------------------
    1        Admins                1
    2        Editors               0
    3        Users                 1

UPDATE roles SET role_status = 0 WHERE id = 2;

yes that exactly what should happen, But I can't make it happen

Link to comment
Share on other sites

7 minutes ago, ginerjm said:

Did THAT query actually run?  Cause the earlier example used 'roles_table' and this used 'roles'

Perhaps if you showed us the code ie, the query, the execution and the check of the results.

This to change the role status ( blocked or not )

$query = "UPDATE roles SET role_status = '{$role_status}' WHERE id='{$db->escape($roles['id'])}' LIMIT 1";

and this to get or call any user with a blocked role ( user_level is the user role and it = the role_id ) so I am getting them through their level which is = role_id which is = '0' or '1' .. I wrote that in the post above, you can checi it

$sql = "SELECT users.id FROM users JOIN roles ON roles.id = users.user_level WHERE roles.role_status = '{$db->escape($role_status)}'";

and both of them are working well by testing them with phpmyadmin, but editors is still active so I know that the solution is with the right function which I can't write it correctly ( and of course I know that I can't use (role_status) directly )

Link to comment
Share on other sites

Would help US if you did an echo of the $query so we can SEE what the query is running.  And the rest that I asked to see.

And what is this "$db->escape() function?  Why are you not using a prepared statement to resolve the arguments?

Link to comment
Share on other sites

5 minutes ago, ginerjm said:

Would help US if you did an echo of the $query so we can SEE what the query is running.  And the rest that I asked to see.

And what is this "$db->escape() function?  Why are you not using a prepared statement to resolve the arguments?

The full code is in the post ( the queries and the function. I don't have anything else for this task

 $db->escape() function is to remove special characters from the value before submitting it to the database 

public function escape($str){
   return $this->con->real_escape_string($str);
}
function real_escape($str){
  global $con;
  $escape = mysqli_real_escape_string($con,$str);
  return $escape;
}

and this is better than the echo

https://drive.google.com/file/d/13NlsUQVqlfRbLWTyqsbONfrhMHTkcm-V/view?usp=sharing

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.