foreach invalid argument and undefined variable
<p>I am trying to create a form and i get an error in these lines.</p>

<pre><code> else 
 {
  //report the errors.

 echo '&lt;h1&gt; Err... &lt;/h1&gt;
 &lt;p&gt; The following error(s) have occured&lt;/p&gt;';

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

 }
</code></pre>

<p>So, what's wrong?? Here's the error message - </p>

<p>Err...</p>

<blockquote>
  <p>The following error(s) have occured -</p>
  
  <p>Notice: Undefined variable: errors in
  C:\wamp\www\password.php on line 107</p>
  
  <p>Warning: Invalid argument supplied for
  foreach() in C:\wamp\www\password.php
  on line 107 Please Try Again.</p>
</blockquote>

<p>I have set errors as an array.</p>

<p>My code above --</p>

<p>if(isset($_POST['submitted'])) 
{</p>

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

$errors = array();

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

{
    $errors[]='Please enter a valid email address.';
}
</code></pre>

<p>Here is my complete code -</p>

<pre><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 '&lt;h3&gt;Your password has been updated.&lt;/h3&gt;';
    }

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

    echo '&lt;p&gt;' .mysqli_error($db_name). 'Query:' . $q.'&lt;/p&gt;';
    }


    exit();
    }
    else 
    {

        //invalid email and password

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

}
    else 
    {
        //report the errors.

    echo '&lt;h1&gt; Err... &lt;/h1&gt;
    &lt;p&gt; The following error(s) have occured&lt;/p&gt;';

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

    }


    ?&gt;
</code></pre>
