Admin Page To Authorise New Users

Hi All

I’m currently working on a site which will have users signing up to become a rep of the company.

Please can someone tell me the best way to approach the following:-

On the admin dashboard, I would like the admin to get a list of all new users who have applied for an account and for admin to approve or reject applications. I will be using a bootstrap framework on the front end and all users will be added to the DB upon application with a status of 0 (inactive) until they are approved to which they then become status 1 (active)

I’ve tried googling but I don’t think I’m asking the right question. If someone could advise or point me in the right direction it would be much appreciated.

Thanks in advance.

The principle is called account activation as far as I know.

There are multiple ways to go about it; setting a flag from 0 to 1 would be one way, but you also consider to create a completely separate table for the applications, since they are actually a different concept from users, so you can evolve them separately if you wanted to.

So when a user applied you would insert their details in user_applications table with a status of “pending”, and then when the admin accepts the application you insert their details into the users table (so they can log in) and set the status in user_application to “accepted”. Otherwise you set the status in the user_applications to “rejected”. You could of course record additional information like when it was accepted/rejected, and if it was rejected, what the reason for the rejection was, etc.

That makes it a lot more explicit what’s actually going on, compared to changing a 0 to a 1.

2 Likes

That’s a great shout regarding the different tables. I hadn’t thought of it like that.
Thanks rpkamp :+1:t2:

1 Like

Hey Rpkamp.

Just revisiting this now I have the admin portal built and running. If I post the dealer application code, would you be able to suggest how I code the set status to pending?

Also would you be able to point me in the right direction as to how I would transfer the data from one table to another once the admin approves the account?

Thanks in advance.

Sure

Well, you select the data from the first table, then insert it into the other table, and then (optionally) remove it from the first table.

Thank you, i’ll get the code posted up this evening.

In regards to transferring the data from one table to another. I was wondering if there was a way to transfer this using a SQL PHP query so that it could be done from one button? i.e the admin clicks a button that approves the account and it automatically transfers the data from the original table to the new table?

Yes you can do that, using the procedure I outlined in my previous post.

Hey Rpkamp, as discussed earlier, this is the code i’m using for new dealer applications. Please can you confirm how i would go about setting the status to pending? (i appreciate that this is a long way to code but i’m still new to PHP and reading up on OOP in the background) :wink:

Thanks in advance.

// Check input errors before inserting in database
    if(empty($name_err) && empty($contactno_err) && empty($email_err)){
        // Prepare an insert statement
        $sql = "INSERT INTO dealerenq (username, password, email, name, companyname, companytype, contactno, toolsowned, vatno) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
         
        if($stmt = mysqli_prepare($con, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sssssssss", $param_username, $param_password, $param_email, $param_name, $param_companyname, $param_companytype, $param_contactno, $param_toolsowned, $param_vatno);
            
            // Set parameters
            $param_username = $username;
			$param_password = $password;
			$param_email = $email;
            $param_name = $name;
            $param_companyname = $companyname;
			$param_companytype = $companytype;
            $param_contactno = $contactno;
			$param_toolsowned = $toolsowned;
			$param_vatno = $vatno;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                echo "Record created successfully"; //Redirect to landing page
                header("location: index.php");
                exit();
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }
         
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Close connection
    mysqli_close($con);
}
?>

Yes you can do that, using the procedure I outlined in my previous post.

Thanks for this, what i meant was would it be something like an SQL INSERT query where it would take the information from one table and insert it into another?

If you are moving data from table to table that is a flag of a bad DB design. Post or PM an SQL dump of your DB with a few sample records and I will review it.

In general I’d agree, but in this case I think it brings a lot of leverage you wouldn’t get if you tried to stuff it all into one table. Did you read the rest of the this thread? (not scan, read)

I would add a column called status and set the value to the string “pending”

Slightly away from the topic, but this bit

echo "Record created successfully"; //Redirect to landing page
header("location: index.php");

isn’t going to redirect, because your echo statement sends output to the browser. When your next line then tries to send a header, it cannot (because sending browser output automatically sends the headers) you should get a “headers already sent” error message.

2 Likes

I didn’t expound on it before, but you could still use a second table for details without moving data. I do it for logging login attempt details and password change attempt details. It is not a second table I was opposed to, just moving data from table to table.

More specific to what the OP posted and your suggestion. When an “Applicant” registers, at minimum, you would have a person table where you would insert all the information about that person. For the admin, there would be a query that queries the users and persons table for person_id’s NOT IN the users table. Admin now has a list of people that are not users. Admin can now accept or reject the person and add them as a user at which point, the columns needed in the users table is minimal. You could have cases where you want people data but they should not be users.

What I am describing very briefly is known as the “Party Model”. I would highly recommend anyone learning it. It will transform the way you architect databases. The model, done right, is infinity scalable.

Check out the books by Len Silverston “The Data Model Resource Book” volume 1,2 and 3.

1 Like

benanamen If you are moving data from table to table that is a flag of a bad DB design. Post or PM an SQL dump of your DB with a few sample records and I will review it.

Thanks Ben. This is the application table.

And this is the table i was planning to use once they were approved…

In regards to the user type i changed from the 1 = user etc to varchar and the field being user or admin. If that makes sense?

droopsnoot

echo "Record created successfully"; //Redirect to landing page header("location: index.php");

isn’t going to redirect, because your echo statement sends output to the browser. When your next line then tries to send a header, it cannot (because sending browser output automatically sends the headers) you should get a “headers already sent” error message.

Oddly i havent had this error come up and it redirects with no issues.

For what you are doing you only need one table with the status being either 1 or 0. If your going to have more than two status’s then you might want to have another table with status_types keyed to the main table. companytype should be a key tied to another table with the company types.

If you were doing an enterprise level application that needed to be able to scale I would suggest using the Party Model.

If toolsowned can be more than one tool then you will need another table to list all the tools owned by whatever id. If there are several tools then you would want yet another table listing all the possible tools and key that to the table that matches them to the right owner or company depending on who owns tools, person or company.

EDIT: See my next post.

Ah, OK, that would explain not seeing it. But I thought it would refuse to send the header after output, so it’s strange that it redirects.

The reason why the OP’s redirect is working is because he has output_buffering set in the php.ini.

OP, turn off output_buffering and fix the code @droopsnoot so wisely pointed out.