-1

When I try to update my database as a check for resetting the password for a user my complete "forgot password" page dies. I really can't see where i went wrong. can someone help me fix this?

function mailer($to, $user)
{
$result = "";
$chars = "abcdefghijklmnopqrstuvwxyz_?!-0123456789";
$charArray = str_split($chars);
for($i = 0; $i < 10; $i++){
    $randItem = array_rand($charArray);
    $result .= "".$charArray[$randItem];
}
$updateStmt = $db -> prepare('UPDATE Gebruiker SET Code = ? WHERE Gebruikersnaam = ?;');
$updateStmt -> execute(array($result, $user));
$updateStmt-> closeCursor();

Error log says the following:

[28-Apr-2016 16:56:47 America/New_York] PHP Notice:  Undefined index: loggedin in /home/joeynlxj/public_html/fnb/layout/header.php on line 71
[28-Apr-2016 16:56:47 America/New_York] PHP Notice:  Undefined variable: db in /home/joeynlxj/public_html/fnb/pages/forgotpass.php on line 11
[28-Apr-2016 16:56:47 America/New_York] PHP Fatal error:  Call to a member function prepare() on a non-object in /home/joeynlxj/public_html/fnb/pages/forgotpass.php on line 11

My database works just fine, it's used through another file.

  • Anything in the error logs? – Jay Blanchard Apr 28 '16 at 21:05
  • Backticks (`) are used for indicating _field names_ in MySQL (see also http://stackoverflow.com/a/11321508/2137833). Is the query being executed at all? – Rick Apr 28 '16 at 21:07
  • 3
    Possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – fusion3k Apr 28 '16 at 21:07
  • I added those backticks later on to see if it would help, so that's not the problem, the query is not being executed at all. but when I remove the `$updateStmt` lines the code works just fine, so i know the problem has to be somewhere in there. – Joey Mariah Apr 28 '16 at 21:29
  • this is the error log `[28-Apr-2016 16:56:47 America/New_York] PHP Notice: Undefined index: loggedin in /home/joeynlxj/public_html/fnb/layout/header.php on line 71 [28-Apr-2016 16:56:47 America/New_York] PHP Notice: Undefined variable: db in /home/joeynlxj/public_html/fnb/pages/forgotpass.php on line 11 [28-Apr-2016 16:56:47 America/New_York] PHP Fatal error: Call to a member function prepare() on a non-object in /home/joeynlxj/public_html/fnb/pages/forgotpass.php on line 11` – Joey Mariah Apr 28 '16 at 21:35
  • `$db` isn't in your function scope – Mark Baker Apr 28 '16 at 21:39
  • `$result .= "".$charArray[$randItem];` and why does your concatenation include an empty string? That's pretty pointless – Mark Baker Apr 28 '16 at 21:40
  • doesn't a require carry through to functions? – Joey Mariah Apr 28 '16 at 21:45
  • @JoeyMariah - Nope, a require doesn't make anything magically global, otherwise you'd never need to pass arguments to any function, and every variable declared in your script would exist in every point of time and space at the same time, and unless you're from Gallifrey, you'd never be able to keep track of what was what without being driven insane by the gibbering horrors of 27-dimensional reality – Mark Baker Apr 28 '16 at 21:46
  • oh okay, i think that might be the problem then... I'll try it out.. – Joey Mariah Apr 28 '16 at 21:48
  • IT WORKS!!! Thank you guys so much! I've been struggling with this for hours now – Joey Mariah Apr 28 '16 at 21:52

1 Answers1

1

Reconnect to the database from within the function.

in my case, all I had to do was add require('../fnb/core/database.php') to my code.