Jump to content

foreach loop error


jimfog
 Share

Recommended Posts

I am using a function that goes updating the password of the user with all the relevant checks in it:

function update_password($post,$email)    {	   global $conn;	   $passerrors=array();	   $current=mysql_real_escape_string($_POST['current']);	   $new=mysql_real_escape_string($_POST['new']);	   $retypednew=mysql_real_escape_string($_POST['retypednew']); 	  	  	  	   if($new!=$retypednew)		 {return $passerrors['notsame']='.........';}	   if(check_passwd($new)==false)		 {return $passerrors['smallpass']='............';}				   $conn->set_charset("utf8");		   $result = $conn->query('select password							    from credentials							    where email="'. $email.'"');		  		   if(!$result){		 throw new Exception('............');		 return false;		  }	    elseif( $result ->num_rows<0)		  {		  return $passerrors['nopassindb']='........';		  }		  else		  {echo 'thank you';}	  	      }

Above, you will not find the update query because I am doing some testing first. The problem is this. I pass the function to a variable and the use a foreach loop to echo if any errors were found:

  $passerrors=update_password($_POST,$_SESSION['valid_user']);				    var_dump( $passerrors);                     foreach ($passerrors as $error) {					   echo $error;					  				   }

I get this message:

Warning: Invalid argument supplied for foreach() in C:\Apache24\htdocs\Appointments\Administrator\profile.php on line 440

The weird is that var_dump DOES output the error founds in the function(for example in the case were the supplied password is below 6 characters). What can be wrong here?

Link to comment
Share on other sites

You THINK you are returning an array with the value of $passerrors. That would look like this:

return $passerrors;

What you are actually returning is the RESULT of an assignment:

return $passerrors['nopassindb']='........'

Since the result of a simple assignment is the value being assigned, you are actually returning '........', which of course cannot be passed on to foreach.

Link to comment
Share on other sites

You THINK you are returning an array with the value of $passerrors. That would look like this:
return $passerrors;

What you are actually returning is the RESULT of an assignment:

return $passerrors['nopassindb']='........'

Since the result of a simple assignment is the value being assigned, you are actually returning '........', which of course cannot be passed on to foreach.

WHat you say is verified by the fact that I used is_array to make some testing and the result was what was returned was not an array-as you say.hm...ok how to fix it?
Link to comment
Share on other sites

I just returned the array after the assignment and that solved the issue. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...