1
votes

I'm working on a script to send a forgot password link to a users email address. I created a table called "recoveryemails_enc" that will hold the users email address, Key, and a expdate. The users email address is pulled from my "users" table. When I run the script, it creates the proper link with the key and expiration date but the data is not being inserted into the database. Here is my code below. I'm not sure what I'm doing wrong.

$result = mysql_query("SELECT email from users WHERE email = '$email' AND active='1'") or die(mysql_error());       
    $expFormat = mktime(date("H"), date("i"), date("s"), date("m")  , date("d")+3, date("Y"));
    $expDate = date("Y-m-d H:i:s",$expFormat);
    $key = md5($email . rand(0,10000) .$expDate);                    
    $no_of_rows = mysql_num_rows($result);                    
    if ($no_of_rows > 0) { 
        $result = mysql_fetch_array($result);      
        $query = mysql_query("INSERT INTO recoveryemails_enc(email, Key, expDate) VALUES('$email', '$key', '$expDate')");
        $passwordLink = "<a href=\"?a=recover&email=" . $key . "&u=" . urlencode(base64_encode($email)) . "\">http://www.mywebsite.com/forgotpw.php?a=recover&email=" . $key . "&u=" . urlencode(base64_encode($email)) . "</a>"; 

Edit command I used to create my table:

CREATE TABLE IF NOT EXISTS `recoveryemails_enc` (  

ID bigint(20) unsigned zerofill NOT NULL auto_increment,
email bigint(20) NOT NULL,
Key varchar(32) NOT NULL,
expDate datetime NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2
Why is a PHP script to insert data into a database tagged as android? - Matt Wolfe
Did you try to directly execute your query? - Thusitha Thilina Dayaratne
Can you provide your table schema? A mismatch is column names/typos will prevent the insert as well. If error reporting is turned off, you won't see helpful messages. - DS.
As a sidenote, you will save debugging time and script processing time, by creating your URL first $url = '?blabla='.$key etc, and then passing it to your <a>: $passwordLink = '<a href="'.$url.'">'.$url.'</a>' - mavrosxristoforos
Learn to use the mysql_error() function to check if your sql statement failed. It might save you lots of debugging time - crafter

2 Answers

3
votes

Your problem lies within the query itself.

$query = mysql_query("INSERT INTO recoveryemails_enc(email, Key, expDate) VALUES('$email', '$key', '$expDate')");

Key is a protected word

try this query instead

$query = mysql_query("INSERT INTO recoveryemails_enc(`email`, `Key`, `expDate`) VALUES('$email', '$key', '$expDate')");
0
votes

Issue is with mysql query, try this:

$query = mysql_query("INSERT INTO recoveryemails_enc(`email`, `Key`, `expDate`) VALUES('".$email."', '".$key."', '".$expDate."')");