jimfog Posted April 9, 2013 Share Posted April 9, 2013 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 More sharing options...
jeffman Posted April 9, 2013 Share Posted April 9, 2013 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 More sharing options...
jimfog Posted April 9, 2013 Author Share Posted April 9, 2013 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 More sharing options...
Ingolme Posted April 9, 2013 Share Posted April 9, 2013 Make the assignment first, return the array after. Link to comment Share on other sites More sharing options...
Rollins Posted April 9, 2013 Share Posted April 9, 2013 assign a value to a key? foreach ($passerrors as $error=>$message) { echo '.$error.' is '.$message.' ; } Link to comment Share on other sites More sharing options...
justsomeguy Posted April 9, 2013 Share Posted April 9, 2013 You can just return an array: return array('nopassindb' = > '........'); Link to comment Share on other sites More sharing options...
jimfog Posted April 10, 2013 Author Share Posted April 10, 2013 I just returned the array after the assignment and that solved the issue. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now