NaderH Posted May 5, 2023 Share Posted May 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/ Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608066 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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; } Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608067 Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 (edited) 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 May 5, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608068 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 (edited) 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 May 5, 2023 by NaderH Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608069 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608070 Share on other sites More sharing options...
Barand Posted May 5, 2023 Share Posted May 5, 2023 Now you know why you get an error when you try using $current_user['role_status'] Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608072 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608073 Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 (edited) As Barand has said - the index you tried to use does not exist in the array. Why are you using it? Edited May 5, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608074 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608076 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608077 Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 And neither do I Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608078 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 I don't know If I can suppose that this is helpful or not. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608084 Share on other sites More sharing options...
Barand Posted May 5, 2023 Share Posted May 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608086 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 (edited) 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 May 5, 2023 by NaderH Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608090 Share on other sites More sharing options...
Barand Posted May 5, 2023 Share Posted May 5, 2023 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; Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608091 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608093 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 21 minutes ago, Barand said: UPDATE roles SET role_status = 0 WHERE id = 2; and that query is working fine also, but editors didn't get blocked Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608094 Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608095 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608096 Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608097 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608099 Share on other sites More sharing options...
NaderH Posted May 5, 2023 Author Share Posted May 5, 2023 Sorry for the quality of the video, it is Google Drive Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608100 Share on other sites More sharing options...
ginerjm Posted May 5, 2023 Share Posted May 5, 2023 All you had to do was add an echo and then cut and paste it to here. Ok - had enough. Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608103 Share on other sites More sharing options...
Barand Posted May 5, 2023 Share Posted May 5, 2023 Did you check what the find_by_id() function wasputting in the array? Quote Link to comment https://forums.phpfreaks.com/topic/316257-php-function-error-notice-undefined-index/#findComment-1608104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.