Saturday, 10 September 2016

php - 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 '

Err...


The following error(s) have occured

';

foreach ($errors as $msg)
{

echo "--$msg
\n";
}
echo '

Please Try Again.


';

}


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 '

Your password has been updated.

';
}


else {
echo '

Whops! Your password cannot be changed due a system error. Try again later. Sorry

';

echo '

' .mysqli_error($db_name). 'Query:' . $q.'

';
}


exit();
}
else

{

//invalid email and password

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

}
else
{

//report the errors.

echo '

Err...


The following error(s) have occured

';

foreach ($errors as $msg)
{
echo "--$msg
\n";
}
echo '

Please Try Again.


';


}


?>

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...