Jump to content

Recommended Posts

Hey guys,

 

I'm trying to make a lost password tool for my site which basically works like so: you enter in your username, you get sent an email with a random key in it and at the same time a temporary record is created in the database of your account ID number and the random key. You can then use these bits of information to set a new password.

 

I have a similar system used to validate people's accounts when they join, and so I assumed it'd be a relatively straightforward job of just editing it to work slightly differently. However, for some reason it's not working. Whenever I try to make the following work, I get this error:

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key) VALUES ('4', '32a63b0d6f450d320821a51965e1b455')' at line 1

 

Those values are the ID number and random key. When I change it so that the key doesn't get sent, the whole thing works perfectly (apart from the fact that the key is not in the database, of course....) so that's definitely where the problem seems to be!

 

Can anybody help me work out what's wrong? I've been staring at it for ages and failed to spot the error!

 

 

//Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); 
mysql_select_db("$db_name")or die("cannot select DB");

// value for username sent from form 
$username=$_POST['username'];

// create a random key to insert into lostpasswords table
$key=md5(uniqid(rand()));

// retrieve the information from the players table for that username
$sql="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);


// if the username has been found, the row must be 1 row 
// keep value in variable name "$count" 
$count=mysql_num_rows($result);

// compare if $count =1 row
if($count==1){

$rows=mysql_fetch_array($result);

// retrieve the player ID and email address
$id=$rows['id'];
$email_to=$rows['email'];

// Insert data into the lostpasswords table for pickup
$sql2 = @mysql_query("INSERT INTO lostpasswords (id, key) VALUES ('$id', '$key')") 
or die("Error: ".mysql_error());
$result2=mysql_query($sql2);

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/206758-need-help-solving-a-syntax-error/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.