开发者

foreach invalid argument and undefined variable

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

Wa开发者_如何学Gorning: 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>';

    }


    ?>


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)


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜