执行

时间:2018-04-17 11:33:00

标签: php

我正在创建一个基于CMS的项目,在这个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" name="groups">
            <option value="MainAdmin">Main Admin</option>
            <option value="Administrator">Administrator</option>
            <option value="ContentCreator1">Shop Content Creator</option>
            <option value="ContentCreator2">Blog Content Creator</option>
            <option value="SocialMediaManager">Social Media Manager</option>
            <option value="Analyst">Analyst</option>
            </select>
        </div>
    </div>
    <div class="box-footer">
        <button name="submit" type="submit" class="btn btn-primary">Submit</button>
    </div>
</form>

如果提交的表单将以操作方式运行:

<?php 
if (isset($_POST['submit'])){
    $username = $_POST['uname'];
    $email = $_POST['email'];
    $password = $_POST['pass'];
    $groups = $_POST['groups'];
    if($groups == "MainAdmin"){
        $level = 0;
    }else if($groups == "Administrator"){
        $level = 1;
    }else if($groups == "ContentCreator1"){
        $level = 2;
    }else if($groups == "ContentCreator2"){
        $level = 3;
    }else if($groups == "SocialMediaManager"){
        $level = 4;
    }else if($groups == "Analyst"){
        $level = 5;
    }
    else{
        $level = Null;
    }
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $notice['email_validation'] = "The email that you have entered is not a valid one";
    }else{
        $registration = new Register();
        $notice[] = $registration->CheckUname($username,$email,$password,$groups,$level);
    }   
}
?>

这是名为Register.class.php的类:

    <?php 
class Register
{   
    protected $notice = array();
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function CheckUname($username,$email,$password,$groups,$level)
    {
        if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level))
        {
            $chk1 = $this->db->prepare("SELECT user_name FROM admins WHERE user_name = ?");
            $chk1->bindParam(1,$username);
            $chk1->execute();
            if($chk1->rowCount() == 1)
            {
                $notice['username_exists'] = "Try different username";
                return $this->notice;
            }else{
                $chk2 = $this->db->prepare("SELECT email_address 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";
                    return $this->notice;
                }else{
                    $this->NewAdmin($username,$email,$password,$groups,$level);
                    $notice['success_message'] = "New admin was successfully added";
                    return $this->notice;
                }
            }
        }
    }
    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();
        }
    }
    public function getNotice()
    {
        return $this->notice;
    }
}
?>

如您所知,如果管理员添加了一个重复用户名或电子邮件地址的新管理员,则会调用通知消息,如果插入成功,则会显示成功消息:

  

$ notice [&#39; username_exists&#39;] =&#34;尝试使用不同的用户名&#34 ;;   $ notice [&#39; email_exists&#39;] =&#34;您输入的电子邮件地址已存在于数据库&#34 ;;   $ notice [&#39; success_message&#39;] =&#34;新管理员已成功添加&#34;;

在课程结束时,我也做了一个这样的方法:

public function getNotice()
{
    return $this->notice;
}

然后我尝试通过这种方式调用通知警告,如果页面上发生任何事情:

<?php 
if(isset($notice['email_validation'])) {
echo "
    <div class='alert alert-danger'>
        <strong>Hey!</strong> ".$notice['email_validation'].".
    </div>
";
}
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>
";
}
if(isset($notice['success_message'])) {
echo "
    <div class='alert alert-success'>
        <strong>Hey!</strong> ".$notice['success_message'].".
    </div>
";
}
?>

但问题是,没有任何消息在页面上根本不出现!

因此,如果您知道如何解决此问题,请告诉我,谢谢......

1 个答案:

答案 0 :(得分:0)

CheckUname()功能中,您可以

$notice['success_message'] = "New admin was successfully added";
return $this->notice;

实际上有两个变量名为&#34; notice&#34;这里涉及的一个是局部变量$notice,它被设置为你的消息,另一个是类成员变量$this->notice,它永远不会被设置但会在这里返回。

您应该使用成员变量并调用getter getNotice()来获取消息字符串,或者使用局部变量并从函数返回它。