Jump to content

NaderH

Members
  • Posts

    24
  • Joined

  • Last visited

Posts posted by NaderH

  1. 13 minutes ago, Barand said:

    Because it uses "SELECT * ... "  it tells us nothing about what the keys are in the returned array. They will be the same as the column names, but we don't know the user table structure.

    That's the main structure

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

    But this this the one I am working on ( for testing)

    Array
    (
        [id] => 
        [username] => 
        [user_level] => 
    )

     

  2. 4 minutes ago, Barand said:

    Because it uses "SELECT * ... "  it tells us nothing about what the keys are in the returned array. They will be the same as the column names, but we don't know the user table structure.

    That's the users table structure

        id       user_name       user_level       
        ---------------------------------------
        1        Admin               1                                 
        2        Editor1             2                                  
        3        User1               3                                  
        4        Editor2             2                                 
        5        User2               3                                   
        6        User3               3 

     

  3. 19 hours ago, Barand said:

    Did you check what the find_by_id() function wasputting in the array?

     

    yes the id of the user from the users table ( if you used it with the users )

     

    function find_by_id($table,$id)
    {
      global $db;
      $id = (int)$id;
        if(tableExists($table)){
              $sql = $db->query("SELECT * FROM {$db->escape($table)} WHERE id='{$db->escape($id)}' LIMIT 1");
              if($result = $db->fetch_assoc($sql))
                return $result;
              else
                return null;
         }
    }

     

  4. 19 hours ago, ginerjm said:

    All you had to do was add an echo and then cut and paste it to here.

    Ok - had enough.

     

    That's the echo code I used for all queries

     

    <p>Role changing query</p>
    <?php
    
    $update = "UPDATE blog.roles SET role_status = '0' WHERE id ='2'";
    
    $uresult = $db->query($update) or die ($update."<br/><br/>".mysql_error());
    
     if($uresult && $db->affected_rows() === 1)
            {
        echo ('Role status changed successfuly'); }
    else {
        echo ('Role status changing failed'); }
    ?>
    
    <br>
    
    <p>Users with role blocked selecting query</p>
    <?php
    
    $selectb = "SELECT users.username FROM blog.users JOIN blog.roles ON roles.id = users.user_level WHERE roles.role_status = '0'";
    
     $sbresult = mysql_query($selectb) or die ($selectb."<br/><br/>".mysql_error());
    
    while($sbrow = mysql_fetch_assoc($sbresult))
        {
        echo $sbrow['username'];    // Print a single column data
        echo print_r($sbrow); }     // Print the entire row data
    ?>
    
    <br>
    
    <p>Users with role allowed selecting query</p>
    <?php
    
    $selecta = "SELECT users.username FROM blog.users JOIN blog.roles ON roles.id = users.user_level WHERE roles.role_status = '1'";
    
     $saresult = mysql_query($selecta) or die ($selecta."<br/><br/>".mysql_error());
    
    while($sarow = mysql_fetch_assoc($saresult))
        {
        echo $sarow['username'];     // Print a single column data
        echo print_r($sarow); }      // Print the entire row data

     

    and that's the output

     

    Role changing query
    Role status changed successfuly
    
    Users with role blocked selecting query
    Editor@userArray ( [username] => Editor@user ) 1
    
    Users with role allowed selecting query
    Admin@userArray ( [username] => Admin@user ) 1User@userArray ( [username] => User@user ) 1

     Actually, I don't know.. aren't you believe that the queries are working or what!!

  5. 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

  6. 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 )

  7. 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

  8. 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 ?

  9. 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 ...

  10. 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.

  11. 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] => 
    )

     

  12. 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;
      }

     

  13. 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.

  14. 30 minutes ago, mac_gyver said:

    programming is an exact science. when someone points out a problem with some code and asks what is that code, it means you need to post exactly the code that was mentioned. if someone asks what is the expected value for something, it means to post exactly what that value is.

    Well, I changed that lint to 

    elseif($login_level['group_status'] != '1')

    instead of 

    elseif($login_level['group_status'] === '0')

    and it caused that all users can login but can't view any page.

  15. 20 minutes ago, mac_gyver said:

    programming is an exact science. when someone points out a problem with some code and asks what is that code, it means you need to post exactly the code that was mentioned. if someone asks what is the expected value for something, it means to post exactly what that value is.

    well, the expected value it should return is 0 or 1 to check if the user is allowed to log in or is banned, but for some reason it doesn't work, I mean if you banned the user he will be able to login.

    consider that I am still a beginner.

  16. 2 hours ago, mac_gyver said:

    the error means that $login_level is a boolean (true or false) value, but the code expects it to be an array with a group_status element.

    either something has changed to cause the find_by_groupLevel() function to return an unexpected value OR that part of the code (which is using an exact comparison with a string consisting of '0') never actually worked, wasn't fully tested, and was probably always producing this error, but the error wasn't being reported/displayed.

    what is the code for the find_by_groupLevel() function and what is the expected value it should return for both a non-banned and a banned user?

    It is to determine if the one who logging in is an admin or a seller or a user to display the home page that should appear to him according to his group(permission) level.

  17. Hello

    I have this error

    Quote

    warning: trying to access array offset on value of type bool in sql.php on line 195

    it appears between all pages while I am navigating from one page to another, this error appears and disappears immediately, but it appears and remains present when viewing sales reports page.

      /*--------------------------------------------------------------*/
      /* Function for checking which user level has access to the page
      /*--------------------------------------------------------------*/
       function page_require_level($require_level){
         global $session;
         $current_user = current_user();
         $login_level = find_by_groupLevel($current_user['user_level']);
         //if user not login
         if (!$session->isUserLoggedIn(true)):
                $session->msg('d','Please Sign in');
                redirect('index.php', false);
          //if Group status Deactive
         elseif($login_level['group_status'] === '0'):  //Line 195
               $session->msg('d','User Banned');
               redirect('home.php',false);
          //checking logged in User level and Require level is Less than or equal to
         elseif($current_user['user_level'] <= (int)$require_level):
                  return true;
          else:
                $session->msg("d", "Error");
                redirect('home.php', false);
            endif;
    
         }

     

×
×
  • 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.