0

I am trying to create a form and i get an error in these lines.

 else 
 {
  //report the errors.

 echo '<h1> Err... </h1>
 <p> The following error(s) have occured</p>';

 foreach ($errors as $msg)
     {
   echo "--$msg<br />\n";
  }
  echo '</p><p>Please Try Again.</p><p><br/></p>';

 }

So, what's wrong?? Here's the error message -

Err...

The following error(s) have occured -

Notice: Undefined variable: errors in C:\wamp\www\password.php on line 107

Warning: Invalid argument supplied for foreach() in C:\wamp\www\password.php on line 107 Please Try Again.

I have set errors as an array.

My code above --

if(isset($_POST['submitted'])) {

require_once('C:\wamp\www\connect.php');
//connecting to db

$errors = array();

if (empty($_POST['email']))

{
    $errors[]='Please enter a valid email address.';
}

Here is my complete code -

//forgot password update

include('C:\wamp\www\header.html');

//check if form has been submitted
require_once('C:\wamp\www\connect.php');
    //connecting to db



if(isset($_POST['submitted'])) {
    $errors = array();


    if (empty($_POST['email']))

    {
        $errors[]='Please enter a valid email address.';
    }

    else
    {
      $e = mysqli_real_escape_string($db_name,trim($_POST['email']));
    }

    //check for current password
    if (empty($_POST['password']))

    {
        $errors[]='Current password does not match.';
    }

    else
    {
      $p = mysqli_real_escape_string($db_name,trim($_POST['password']));
    }

    //check for a new password and match with confirm pass.

    if(!empty($_POST['password1']))
    {
        if($_POST['password1'] != $_POST['cpass'])
        {
            $errors[] = 'The entered password and confirm password do not match.';
        }
        else
        {
            $np=mysqli_real_escape_string($db_name,trim($_POST['password1']));
        }
    }
    if(empty($errors))
    //if everything is fine.

    //verify the entered email address and password.

    $q="SELECT username FROM users WHERE (email='$e' AND password=SHA1('$p'))";
    $r=@mysqli_query($db_name,$q);
    $num = @mysqli_num_rows($r);
    if($num==1)
    //if it matches.

    //get user id
    {
    $row=mysqli_fetch_array($r, MYSQLI_NUM);

    //udpdate query.

    $q="UPDATE users SET password= SHA1('$np') WHERE username=$row[0]";

    $r=@mysqli_query($db_name, $q);

    if (mysqli_affected_rows($db_name) ==1)

    {
        echo '<h3>Your password has been updated.</h3>';
    }

    else {
        echo '<h3>Whops! Your password cannot be changed due a system error. Try again later. Sorry</h3>';

    echo '<p>' .mysqli_error($db_name). 'Query:' . $q.'</p>';
    }


    exit();
    }
    else 
    {

        //invalid email and password

        echo 'The email address and password do not match';
    }

}
    else 
    {
        //report the errors.

    echo '<h1> Err... </h1>
    <p> The following error(s) have occured</p>';

    foreach ($errors as $msg)
        {
            echo "--$msg<br />\n";
        }
        echo '</p><p>Please Try Again.</p><p><br/></p>';

    }


    ?>
5
  • What's wrong is, not to state the obvious, the variable $errors is not defined, and therefore is not an array that can be iterated by foreach. If you can provide more detail on your code prior to this we may be able to help. Commented Oct 21, 2010 at 21:31
  • It basically says: Undefined variable: errors, which means that you have an undefined variable $errors. Commented Oct 21, 2010 at 21:31
  • So, does the else come right after that if? Commented Oct 21, 2010 at 21:34
  • i have added my complete code. Commented Oct 21, 2010 at 21:39
  • 1
    Is there a reason why you edited your question to delete the content? Commented Mar 14, 2011 at 13:32

5 Answers 5

3

There is no array named $errors. You will have to look further up your script why not.

You can fix the error message by using

if (!empty($errors) and (is_array($errors)))
 foreach ($errors as $msg)
Sign up to request clarification or add additional context in comments.

6 Comments

I have set errors....here's a preview of my code above... if(isset($_POST['submitted'])) { require_once('C:\wamp\www\connect.php'); //connecting to db $errors = array(); if (empty($_POST['email'])) { $errors[]='Please enter a valid email address.'; }
@Johnson strange. Can you post the full code as it is, in a separate code block?
@Johnson you are doing the $errors = array(); at the wrong place, inside the condition checking for isset($_POST['submitted']). You need to take it outside into the main script
if i do that, the else part of Err...the following error(s) have occured is still displayed, but the errors are gone.
@Johnson that is because you are showing that block when $_POST["submitted"] is not set. Doesn't sound like that's what you want to do
|
2

Your foreach loop is out of the scope in regards to where the $error array is defined.

Your code in a nutshell:

if(isset($_POST['submitted'])) {
    $errors = array();
} else {
    foreach($errors as $error)
}

If $_POST is not set, than your $errors is not defined.

Comments

2

Move your declaration for "$errors = array()" above the line "if(isset($_POST['submitted'])) { " and everything should work fine!

Comments

1

You have two problems. The first is the cause of the empty/non-existent array and the second is a lack of testing for it.

The first is that you are testing for errors inside of an if block and then looping through them inside of the else block.

if (isset($_POST['submitted'])) {
  // create errors array and set errors
} else {
  // loop through array of errors
}

So if errors are set, the script doesn't make it to the loop. If the script makes it to the loop, no errors were set.

The second is that you should only enter the foreach loop after you have tested the array:

if (!empty($errors) && is_array($errors)) { // use this line and get rid of the else.
  foreach ($errors as $msg) {
        echo "--$msg<br />\n";
  }
  echo '</p><p>Please Try Again.</p><p><br/></p>';
 } // and close it.

Comments

0

Basically, what's happening here is you're using $errors before it is defined.

It may be that you need to set "$errors = array( )" near the top of your script so that it is always at least an empty array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.