0

I'm making my own custom CMS using PHP Object Oriented... I have made a page where a main admin of a site can add another admin. In order to do this I made this form:

<form role="form" method="POST" action="">
    <div class="box-body">
        <div class="form-group">
            <label>User name</label>
            <input type="text" class="form-control" placeholder="Enter username" name="uname" required>
        </div>
        <div class="form-group">
            <label for="exampleInputEmail1">Email address</label>
            <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" name="email" required>
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">Temporary password</label>
            <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Enter password" name="pass" required>
        </div>
        <div class="form-group">
            <label>Group admin</label>
            <select class="form-control">
                <option>Main Admin</option>
                <option>Administrator</option>
                <option>Content Creator</option>
                <option>Analyst</option>
            </select>
        </div>
    </div>
    <div class="box-footer">
        <button name="submit" type="submit" class="btn btn-primary">Submit</button>
    </div>
</form>

Then I added this as action file:

if (isset($_POST['submit'])) {
    $username = $_POST['uname'];
    $email = $_POST['email'];
    $password = $_POST['pass'];
    $registration = new Register();
    $registration->CheckUname($username,$email);
}

So as you can see I have called a class called Register and in this class I coded this:

class Register {
    private $db;

    public function __construct() {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }

    public function CheckUname($username,$email) {
        if(!empty($username)&&($email)) {
            $chk1 = $this->db->prepare("SELECT username FROM admins WHERE user_name= ?");
            $chk1->bindParam(1,$username);
            $chk1->execute();
            if($chk1->rowCount() == 1)
            {
                $notice['username_exists'] = "Try different username";
            } else {
                $chk2 = $this->db->prepare("SELECT email FROM admins WHERE email_address= ?");
                $chk2->bindParam(1,$email);
                $chk2->execute();
                if($chk2->rowCount() == 1)
                {
                    $notice['email_exists'] = "The email address that you have entered is already exists in database";
                }else{
                    // I want to call the NewAdmin function here
                }
            }
        }
    }

    public function NewAdmin($username,$email,$password) {
        if(!empty($username)&&!empty($email)&&!empty($password)) {
            $reg = $this->db->prepare("INSERT INTO admins (user_name, email_address, password_hash) VALUES ( '?', '?', '?')");
            $reg->bindParam(1,$username);
            $reg->bindParam(2,$email);
            $reg->bindParam(2,$password);
            $reg->execute();
        }
    }
}

Basically I tried to check if that username already exists or not then I also checked if the email exists and if not I want to call the NewAdmin function so the admin can be inserted to db. The problem is I don't know how to do that inside of the CheckUname function. Any help please ?

Also I've got another question which is why the errors that should be produced when the user enter a username or email that already exists in database does not shown! However I have set a variable for this:

if($chk1->rowCount() == 1) {
    $notice['username_exists'] = "Try different username";
} else {
    $chk2 = $this->db->prepare("SELECT email FROM admins WHERE email_address= ?");
    $chk2->bindParam(1,$email);
    $chk2->execute();
    if($chk2->rowCount() == 1) {
        $notice['email_exists'] = "The email address that you have entered is already exists in database";
    } else {
        // I want to call the NewAdmin function here
    }
}

I have also set the error in the page like this:

if(isset($notice['username_exists'])) {
    echo "<div class='alert alert-danger'><strong>Hey!</strong> ".$notice['username_exists'].".</div>";
}
if(isset($notice['email_exists'])) {
    echo "<div class='alert alert-danger'><strong>Hey!</strong> ".$notice['email_exists'].".</div>";
}

But no errors appears on the page while submitting a username or an email that exists in the db!

3
  • 1
    $this->NewAdmin($username,$email,$password) replace parameters with actual variables. Commented Sep 13, 2016 at 12:20
  • Is $notice a local variable in the function? Commented Sep 13, 2016 at 12:24
  • You're going to need to pass $password to CheckUname when you call it if you want to then call NewAdmin within CheckUname Commented Sep 13, 2016 at 12:26

1 Answer 1

4

Use $this->NewAdmin($username,$email,$password) inside the CheckUname() function of Register class. You can call same class function inside another class by using $this. $this represents the current class object.

I am just learning to use stack overflow, please don't approve this edit of mine...! Thanks..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.