尝试访问 bool PHP 类型值的数组偏移量 [重复]

Posted

技术标签:

【中文标题】尝试访问 bool PHP 类型值的数组偏移量 [重复]【英文标题】:Trying to access array offset on value of type bool PHP [duplicate] 【发布时间】:2021-09-16 18:04:40 【问题描述】:

当我想注册一个新用户时出现错误

注意:尝试访问第 37 行 P:\xampp\htdocs\BTP\v4\register.php 中 bool 类型值的数组偏移量

注意:尝试访问第 39 行 P:\xampp\htdocs\BTP\v4\register.php 中 bool 类型值的数组偏移量

不过,一切正常,用户创建 但我想修复它,我不知道出了什么问题

<?php
require_once 'include/database.php';

if(isset($_POST['btn_register']))

    $fname = strip_tags($_REQUEST['fname']);
    $lname = strip_tags($_REQUEST['lname']);
    $email = strip_tags($_REQUEST['email']);
    $phone = strip_tags($_REQUEST['phone']);
    $password = strip_tags($_REQUEST['password']);
    $rpassword = strip_tags($_REQUEST['passwordrepet']);

    if(empty($fname))
        $errorMsg[] = "Proszę podać imię";
    else if(empty($lname))
        $errorMsg[] = "Proszę podać nazwisko";
    else if(empty($email))
        $errorMsg[] = "Proszę podać adres e-mail";
    else if(empty($password))
        $errorMsg[] = "Proszę podać hasło";
    else if(empty($rpassword))
        $errorMsg[] = "Należy ponownie podać hasło";
    else if(strlen($password) < 6)
        $errorMsg[] = "Hasło musi zawierać conajmniej 6 znaków";
    else if($password != $rpassword)
        $errorMsg[] = "Hasła nie są takie same";
    else

        try
            $select_stmt = $db->prepare("SELECT email, phone FROM user WHERE email=:email OR phone=:phone");
            $select_stmt->bindValue(':email', $email, PDO::PARAM_STR);
            $select_stmt->bindValue(':phone', $phone, PDO::PARAM_STR);
            $select_stmt->execute();

            $row = $select_stmt->fetch(PDO::FETCH_ASSOC);
        
            if($row['email'] == $email)
                $errorMsg[] = "Adres e-mail jest już zajęty";
            else if($row['phone'] == $phone)
                $errorMsg[] = "Numer telefonu został juz podany do innego konta";
            else if(!isset($errorMsg))

                $new_password = password_hash($password, PASSWORD_DEFAULT);

                $insert_stmt = $db->prepare("INSERT INTO user (fname, lname, email, phone, password) VALUES (:fname, :lname, :email, :phone, :password)");

                if($insert_stmt->execute(array( ':fname' =>$fname,
                                                ':lname' =>$lname,
                                                ':email' =>$email,
                                                ':phone' =>$phone,
                                                ':password' => $new_password)))

                    $registerMsg = "Udało sie zarejestrować";
                

            

        catch(PDOException $e)
            echo $e->getMessage();
        
    

?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Best Trip Partner Register</title>

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/main.css" rel="stylesheet">

</head>

<body class="bg-gradient-primary">

    <div class="container">

        <div class="card o-hidden border-0 shadow-lg my-5">
            <div class="card-body p-0">
                <!-- Nested Row within Card Body -->
                <div class="row">
                    <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
                    <div class="col-lg-7">
                        <div class="p-5">
                            <div class="text-center">
                                <h1 class="h4 text-gray-900 mb-4">Utwórz konto</h1>
                            </div>
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" class="user">
                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                        <input type="text" name="fname" class="form-control form-control-user" id="exampleFirstName" placeholder="Imię">
                                    </div>
                                    <div class="col-sm-6">
                                        <input type="text" name="lname" class="form-control form-control-user" id="exampleLastName" placeholder="Nazwisko">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                        <input type="email" name="email" class="form-control form-control-user" id="exampleInputEmail" placeholder="Adres e-mail">
                                    </div>
                                    <div class="col-sm-6">
                                        <input type="phone" name="phone" class="form-control form-control-user" id="exampleInputPhone" placeholder="Numer telefonu">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                        <input type="password" name="password" class="form-control form-control-user" id="exampleInputPassword" placeholder="Hasło">
                                    </div>
                                    <div class="col-sm-6">
                                        <input type="password" name="passwordrepet" class="form-control form-control-user" id="exampleRepeatPassword" placeholder="Powtórz hasło">
                                    </div>
                                </div>

                                <?php
                                print_r($row);
                                    if(isset($errorMsg))
                                        foreach($errorMsg as $error)
                                        ?>
                                            <div class="alert alert-danger">
                                                <strong><?php echo $error; ?></strong>
                                            </div>
                                        <?php
                                        
                                    
                                    if(isset($registerMsg))
                                        ?>
                                            <div class="alert alert-success">
                                                <strong><?php echo $registerMsg; ?></strong>
                                            </div>
                                        <?php
                                    
                                ?>
                                <button type="submit" name="btn_register" class="btn btn-primary btn-user btn-block">Zarejestruj się</button>

                            </form>
                            <hr>
                            <div class="text-center">
                                <a class="small" href="forgot-password.php">Zapomniałeś hasła?</a>
                            </div>
                            <div class="text-center">
                                <a class="small" href="login.php">Masz już konto? Zaloguj się!</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Bootstrap core javascript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>

</body>

</html>

【问题讨论】:

如果 fetch 失败,则返回 false。 但是当我执行 echo $row['email'] 时,我看到了电子邮件,它是正确的 您可以执行 print_r($row) 来验证返回的内容。 结果数组([电子邮件] => john@mail.com [电话] => 123456123) 好吧,提交表单后,并没有显示其中包含的内容,但我不知道为什么。现在我编辑并显示所有代码,有人看到 waht 是错误的。我已经没有力气了 【参考方案1】:

请使用array_key_exists() 函数检查您的变量中是否存在关键电子邮件和电话。 比如……

if (array_key_exists("email", $row["email"])) 
   if ($row['email'] == $email) 
      $errorMsg[] = "Adres e-mail jest już zajęty";
 

【讨论】:

警告:array_key_exists() 期望参数 2 是数组,字符串在 P:\xampp\htdocs\BTP\v4\register.php 中的第 36 行 if(array_key_exists($row["email" ], $row['phone']))

以上是关于尝试访问 bool PHP 类型值的数组偏移量 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

当php中的表中没有记录时,尝试访问bool类型值的数组偏移量

试图在php中访问bool类型值的数组偏移量[重复]

Laravel Newsletter 包错误:尝试访问 bool 类型值的数组偏移量

警告:尝试访问第 41 行 C:\xampp\htdocs\crud\read.php 中 null 类型值的数组偏移量 [关闭]

尝试访问laravel中null类型值的数组偏移量

重复错误:“不再支持带有花括号的数组和字符串偏移量访问语法”与 phpmyadmin