I'm very new to all of these things and I'm just really stumped on this. I've been trying for a day and a half to get this part of the code to work, and I've tried numerous different things. It's just not wanting to work for me.
Here's the whole script
<?php
$dbusername = "****"; // info works to connect to login
$dbpassword = "****"; // and everything works fine retrieving
$dbhost = "localhost"; // the email to send the code to (which all works)
$dbname = "****";
try {
$conn = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbusername, $dbpassword);
}
catch(PDOException $ex)
{
$msg = "Failed to connect to the database";
}
function getToken($length=32){
//redacted - working and unrelated, suffice it to say the token returns properly
return $token;
}
if (isset($_POST["ForgotPassword"])) {
if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$email = $_POST["email"];
}else{
echo "Email is invalid.";
exit;
}
// Check to see if a user exists with this e-mail
$query = $conn->prepare('SELECT email FROM users WHERE email = :email');
$query->bindParam(':email', $email);
$query->execute();
$userExists = $query->fetch(PDO::FETCH_ASSOC);
$conn = null
if ($userExists["email"])
{
$resetpass = getToken();
try {
$conn = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbusername, $dbpassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('UPDATE users SET passwordreset=:resetpass WHERE email=:email');
$stmt->bindParam(':resetpass', $resetpass);
$stmt->bindParam(':email', $email);
$stmt->execute();
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage(); //$sql not set anymore
}
$conn = null;
// Create a url which we will direct them to reset their password
$pwrurl = "*******/reset_password.php?q=".$resetpass;
// Mail them their key
$mailbody = "redacted \n\n" . $pwrurl;
mail($userExists["email"], "redacted", $mailbody);
echo "Your password recovery key has been sent to your e-mail address.";
}
else
echo "No user with that e-mail address exists.";
}
?>
Without this query, everything else works famously. It breaks and won't continue here. It never echos the success or failure.
Edit Here's the HTML form too
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Forgot Password</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<form action="change.php" method="POST">
<table align="center" width="30%" border="0">
<div>
<tr>
<td><input type="text" name="email" placeholder="[email protected]" required /></td>
</tr>
<tr>
<td><button type="submit" name="ForgotPassword" value=" Request Reset ">Reset</button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
$sql.