【问题标题】:Weird Undefined variable php error [duplicate]奇怪的未定义变量php错误[重复]
【发布时间】:2018-03-05 03:35:00
【问题描述】:

我正在使用 PHP OOP 开发 CMS。在这个 CMS 中,有一个网站管理员可以添加另一个管理员的功能。为此,我创建了一个表单并添加了操作。该文件名为admin_new.php,如下所示:

    <?php 
if (isset($_POST['submit'])){
    $username = $_POST['uname'];
    $email = $_POST['email'];
    $password = $_POST['pass'];
    $groups = $_POST['groups'];
    if($groups == "Main Admin"){
        $level = 1;
    }else if($groups == "Administrator"){
        $level = 2;
    }else if($groups == "Content Creator"){
        $level = 3;
    }else if($groups == "Social Media Manager"){
        $level = 4;
    }else{
        $level = 5;
    }
    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);
    }   
}
?>
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            Add New Admin
            <small>You can add new admin here</small>
        </h1>
        <ol class="breadcrumb">
            <li class="active">addnewadmin.php</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-6">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Required Information</h3>
                    </div>
                    <?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>
                        ";
                    }
                    ?>
                    <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="Main Admin">Main Admin</option>
                                    <option value="Administrator">Administrator</option>
                                    <option value="Content Creator">Content Creator</option>
                                    <option value="Social Media Manager">Social Media Manager</option>
                                    <option value="Analyst">Analyst</option>
                                </select>
                            </div>
                        </div>
                        <div class="box-footer">
                            Visit <a href="https://zite.pouyavagefi.com/documentation/types.php">admin types</a> documentation to know the differences between each admin.
                        </div>
                        <div class="box-footer">
                            <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
</div>

如你所见,我调用了一个名为 Register 的类,而这个类也放在这里:

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

所以它看起来基本上很好,但唯一的问题是,每当我尝试在表单中提交时,我都会收到这个错误:

未定义变量:admin_new.php 第 22 行中的组‌​

admin_new.php22 行是这样的:

$notice = $registration->CheckUname($username,$email,$password,$groups‌​,$level);

所以您可以在代码中看到我已经定义了 $groups 变量,并且它在表单中获取了 groups 值。因此我真的不知道为什么会出现这个错误?!

因此,如果您知道我应该做什么或我的错,请告诉我.. 我真的很感激。谢谢

【问题讨论】:

  • 您是否尝试输出每个变量以检查它们是否包含某些内容并从那里开始?
  • 不过,与错误不同的是,您永远不会将 $this-&gt;notice 返回为 null,因为您声明 $notice 而不是 $this-&gt;notice['param'] = 'arg'
  • 伙计们,你能写出你的 cmets 作为答案吗!我无法理解您在此评论部分中所说的内容

标签: php oop mysqli


【解决方案1】:

您收到此错误,因为 $groups in call

$notice = $registration->CheckUname($username,$email,$password,$groups‌​‌​,$level); 

$groups 以不同的编码编写,并且具有非 ASCII 字符。可能一个或多个字母是用不同的语言输入的。只需以普通拉丁文输入或从上面提到的变量之一复制过来。 这应该可以解决问题。

编辑: 除了未来的问题,如果你偶然发现类似的问题,只需将代码转换为 ASCII 编码并返回 UTF-8,所有非 ASCII 字符将被替换为一些通用替换,如下划线或问号

【讨论】:

  • 如何检查它是否非ascii
  • @chris85,尝试在此处复制粘贴该部分代码:utils.paranoiaworks.org/diacriticsremover
  • 你能否就你的谈话内容发表一些评论或文章
  • 啊,我明白了,有尾随内容。
  • @wrephebiajou 在$groups 之后和, 之前你有一个非ASCII 字符。
【解决方案2】:

在下面的函数中,您使用了$groups1 而不是$groups >> 更新为$groups

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,$groups1);
            $reg->bindParam(5,$level);
            $reg->execute();
        }
    }

【讨论】:

  • 你为什么不在你的答案中更正?
  • 那只是为了测试问题。现在问题已更新,问题存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多