如何在PHP OOP

时间:2016-09-13 12:18:42

标签: php mysql

我正在使用PHP面向对象制作我自己的自定义CMS ...我创建了一个页面,其中网站的主管理员可以添加另一个管理员。为了做到这一点,我做了这个表格:

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

然后我将其添加为操作文件:

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

因为你可以看到我调用了一个名为Register的类,在这个类中我编写了这个:

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();
        }
    }
}

基本上我试图检查该用户名是否已经存在,然后我还检查了电子邮件是否存在,如果不是,我想调用NewAdmin函数,以便管理员可以插入到db。问题是我不知道如何在CheckUname函数内执行此操作。有什么帮助吗?

此外,我还有另一个问题,即用户输入已存在于数据库中的用户名或电子邮件时应该产生的错误未显示的原因!但是我为此设置了一个变量:

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

我还在页面中设置了错误:

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>";
}

但在提交db中存在的用户名或电子邮件时,页面上不会显示错误!

1 个答案:

答案 0 :(得分:4)

$this->NewAdmin($username,$email,$password)课程的CheckUname()功能中使用Register。您可以使用$this在另一个类中调用相同的类函数。 $this表示当前的类对象。