duangao7133 2018-04-17 14:32
浏览 18

如何在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?

  • 写回答

2条回答 默认 最新

  • dongtaogou6226 2018-04-17 14:38
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题