0

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 ?>
Aina
  • 85
  • 1
  • 9
  • in errors.php you talk about `$errors`, but nowhere is errors defined – delboy1978uk Jul 26 '18 at 13:58
  • @delboy1978uk In registration first line – Andreas Jul 26 '18 at 13:58
  • thats a different file. unless you include errors php from that file, you are starting a fresh script – delboy1978uk Jul 26 '18 at 14:01
  • @delboy1978uk OP says she runs index.php, which includes first registration.php, then errors.php. – Andreas Jul 26 '18 at 14:02
  • OP: Place an `echo "registration";` in registration.php. That way you can see if the file is loaded. Also do you have errors turned off? – Andreas Jul 26 '18 at 14:06
  • 1
    @Andreas the echo did not work so I suppose the problem is in this file. I found out that I misspelled by adding a space after the first question mark. My problem is solved, I have a new one, but I will get through that by my self. Thank you though :D If I hadn't tried to echo something, I would have sat here for couple more hours – Aina Jul 26 '18 at 14:15
  • replace `include` with `require`. – delboy1978uk Jul 26 '18 at 14:33
  • @Aina take this as an important lesson in debugging. Never assume something is as you think. Always double check. In this case it was to double check that a file was loaded, next time it may be that the contents of a variable is as you think. – Andreas Jul 26 '18 at 15:01
  • @Andreas thank you, next time I will double check everything. – Aina Jul 26 '18 at 15:05

0 Answers0