I just started to learn PHP so I'm sorry if this is a dumb problem. I was following a tutorial to create login/registration form that saves data in Mysql DB using PHP.
So I have 3 files:
- First, that has the main form: index.php
- Second, that has the functionality of the form. The array that holds the errors is defined and also filled with information in this file: registration.php
- Third, the file that has a code that displays errors (from the array that is defined and filled in the file: registration.php) in HTML code: errors.php
in the index.php file, I include the registration.php on the top of the file, but the file errors.php is included in the specific spot in the code.
In that tutorial, he has no errors, but I have. And yes I compared my code to the tutorials code, couldn't find a problem in mine though.
The full errors is:
Notice: Undefined variable: errors in C:\xampp\htdocs\Project\errors.php on line 1 Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\Project\errors.php on line 1
A part from the index.php
<?php include('registration.php') ?>
<form action="registration.php" method="post">
              <?php include('errors.php'); ?>// here I include the error file
               <div>
                    <input type="text" id="IpNmSing" required name="username">
                    <label for="">Name</label>
                </div>
                <div>
                    <input type="email" id="IpEmSign" required name="email">
                    <label for="">Email</label>
                </div>
                <div>
                    <input type="password" id="IpPswSing" required name="password">
                    <label for="">Password</label>
                </div>
                <div>
                    <div class="signinBtn">
                       <input type="submit" value="Sign Up" name="reg_user">
                    </div>
                </div>
 </form>
A part from the registration.php
$errors = array(); 
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['password']);
// checkinf if there are any walues, if not the error is pushed inside the arr
//one of the parts that fills the array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
And the errors.php file
    <?php  if (count($errors) > 0) : ?>
  <div class="error">
    <?php foreach ($errors as $error) : ?>
      <p><?php echo $error ?></p>
    <?php endforeach ?>
  </div>
<?php  endif ?>
