-2

When I want to register a new user I have error

Notice: Trying to access array offset on value of type bool in P:\xampp\htdocs\BTP\v4\register.php on line 37

Notice: Trying to access array offset on value of type bool in P:\xampp\htdocs\BTP\v4\register.php on line 39

Still, it all works, and user create but I want to fix it and I don't know what's wrong

<?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>
7
  • If fetch fails, it'll return false. Commented Sep 16, 2021 at 18:10
  • But when i do echo $row['email'] I see email and it's correct Commented Sep 16, 2021 at 18:11
  • You can do print_r($row) to verify what's being returned. Commented Sep 16, 2021 at 18:14
  • Result Array ( [email] => [email protected] [phone] => 123456123 ) Commented Sep 16, 2021 at 18:18
  • Okay, after submitting the form, it doesn't show what it contains, but I don't know why. Now i edit and show all code meaby someone see waht is wrong. I don't have the strength anymore Commented Sep 16, 2021 at 18:53

1 Answer 1

-2

Please use array_key_exists() function to check if the key email and phone exist in your variable. Something like...

if (array_key_exists("email", $row)) {
   if ($row['email'] == $email) {
      $errorMsg[] = "Adres e-mail jest już zajęty";
 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Warning: array_key_exists() expects parameter 2 to be array, string given in P:\xampp\htdocs\BTP\v4\register.php on line 36 if(array_key_exists($row["email"], $row['phone'])){
it makes no sense to check if the key email and phone exist in your variable. they always exist, given query returned any rows

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.