user9658320
user9658320

Reputation:

How to return success message after the process is done in PHP OOP

I have a Class named Register.class.php which goes like this:

    <?php 
class Register
{   
    private $db;
    public function __construct()
    {
       $this->db = new Connection();
       $this->db = $this->db->dbConnect();
    }
    public function NewAdmin($username,$email,$password,$groups,$level)
    {
        if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level))
        {
            $reg = $this->db->prepare("INSERT INTO admins (user_name, email_address, password_hash, group_admin, date_joined, admin_level) VALUES ( ?, ?, ?, ?, NOW(), ?)");
            $reg->bindParam(1,$username);
            $reg->bindParam(2,$email);
            $reg->bindParam(3,$password);
            $reg->bindParam(4,$groups);
            $reg->bindParam(5,$level);
            $reg->execute();
        }
    }
}
?>

And I call this method like this:

$registration = new Register();
$registration->NewAdmin($username,$email,$password,$groups,$level);

But now I want to show a success message like New Admin Has Been Added inside the NewAdmin method...

So please can you tell me how can I do that?

Upvotes: 0

Views: 241

Answers (2)

John
John

Reputation: 1319

It depends if you are using PHP 7.2

 public function NewAdmin($username,$email,$password,$groups,$level):bool
{
    if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level))
    {
        $reg = $this->db->prepare("INSERT INTO admins (user_name, email_address, password_hash, group_admin, date_joined, admin_level) VALUES ( ?, ?, ?, ?, NOW(), ?)");
        $reg->bindParam(1,$username);
        $reg->bindParam(2,$email);
        $reg->bindParam(3,$password);
        $reg->bindParam(4,$groups);
        $reg->bindParam(5,$level);
        return $reg->execute();
   }
   return false;
}


$registration = new Register();
$test = $registration->NewAdmin($username,$email,$password,$groups,$level);
echo ($test ? "TRUE ADMIN ADDED" : "FALSE ADMIN NOT CREATED");

Also check the manual at http://php.net/manual/en/functions.returning-values.php for more documentation.

Do note if you are using PHP7.1 < the ':bool' return type can not be used on the function.

Upvotes: 1

Pantoflarz
Pantoflarz

Reputation: 76

Could you not do a simple

return "New Admin created";

inside the function, upon checking the action was successful?

Please bear in mind that at the moment your code relies on everything working, you are not checking if anything actually succeeded, you are not catching any exceptions and you are most definitely not dealing with any errors.

Moreover, you could then extend this and return different messages depending on errors, exceptions or missing values.

I would not rely on !empty as the only way of verifying my variables either, unless they are known to be correct by some other algorithm or part of the website (if you do not verify them, I would suggest using some sort of library or php built in functions as a minimum).

Upvotes: 0

Related Questions