You have an error in your SQL syntax near...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
netpro25
Forum Newbie
Posts: 11
Joined: Wed Sep 21, 2005 4:47 pm

You have an error in your SQL syntax near...

Post by netpro25 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Okay so when i run this script I get the following error: "You have an error in your SQL syntax near 'key = xxx' at line 1" It does everything it is supposed to do but update the keymail table used value to "5". Anyone know what is wrong?

Code: Select all

<?php
$email=$_POST['email'];

//Connect to database with connection script file
require 'includes/db_connect.php';

//used is not equal to zero
$sql="SELECT * FROM keymail WHERE used != '0' LIMIT 1";
$query=mysql_query($sql);
$result=mysql_fetch_object($query);

echo ("Thank you for purchasing a CD key your key is $result->key . You will also recieve an e-mail with this cd key, thank you again.");

$message="Your cd key is $result->key . Thank you again.";
$headers='From: myself@now.com'."\r\n" . ' Reply-To: stillme@now.com' ."\r\n";
$subject="Thank you for your purchase.";

mail($email,$subject,$message,$headers);

$secondsql="UPDATE keymail SET used='5' WHERE key = $result->key";
$secondquery=mysql_query($secondsql)
or die(mysql_error());

?>



Thanks so much
Marcel


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

surround $result->key with ' ', i.e. '$result->key' , also if 5 represents used you need to make used != 0 read used !=5 :-D, and you need to change the myself@my.com to your actual e-mail address, did you even read the script man?:-D I posted tha tfor you a few days ago telling you it needed to be modified;)
netpro25
Forum Newbie
Posts: 11
Joined: Wed Sep 21, 2005 4:47 pm

Post by netpro25 »

yea I read it, but I have not changed the email address yet, i first want it working before I got into doing all of that. I have been concentrating on getting it going.

thanks dude, you are a life saver
Marcel
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

making sure is all :-D
netpro25
Forum Newbie
Posts: 11
Joined: Wed Sep 21, 2005 4:47 pm

Post by netpro25 »

I hate to drag this on but I am still getting the same error. Here is what I have now..

Code: Select all

<?php
$email=$_POST['email'];

//Connect to database with connection script file
require 'includes/db_connect.php';

//used is not equal to zero
$sql="SELECT * FROM keymail WHERE used != '0' LIMIT 1";
$query=mysql_query($sql);
$result=mysql_fetch_object($query);

echo ("Thank you for purchasing a CD key your key is $result->key . You will also recieve an e-mail with this cd key, thank you again.");

$message="Your cd key is $result->key . Thank you again.";
$headers='From: mmanning@nexgentec.com'."\r\n" . ' Reply-To: mmanning@nexgentec.com' ."\r\n";
$subject="Thank you for your purchase.";

mail($email,$subject,$message,$headers);

$secondsql="UPDATE keymail SET used=0 WHERE key = '$result->key'";
$secondquery=mysql_query($secondsql)
or die(mysql_error());

?>
Output is

Code: Select all

Thank you for purchasing a CD key your key is xxx . You will also recieve an e-mail with this cd key, thank you again.You have an error in your SQL syntax near 'key = 'xxx'' at line 1
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

'key' is a MySQL keyword..

Code: Select all

<?php
$email=$_POST['email'];

//Connect to database with connection script file
require 'includes/db_connect.php';

//used is not equal to zero
$sql="SELECT * FROM `keymail` WHERE `used` != '0' LIMIT 1";
$query=mysql_query($sql);
$result=mysql_fetch_object($query);

echo ("Thank you for purchasing a CD key your key is {$result->key} . You will also recieve an e-mail with this cd key, thank you again.");

$message="Your cd key is {$result->key} . Thank you again.";
$headers='From: mmanning@nexgentec.com'."\r\n" . ' Reply-To: mmanning@nexgentec.com' ."\r\n";
$subject="Thank you for your purchase.";

mail($email,$subject,$message,$headers);

$secondsql="UPDATE `keymail` SET `used`=0 WHERE `key` = '{$result->key}'";
$secondquery=mysql_query($secondsql)
or die(mysql_error());

?>
netpro25
Forum Newbie
Posts: 11
Joined: Wed Sep 21, 2005 4:47 pm

Post by netpro25 »

Dude, you are awsome.... You dont want to know how long I have spent trying to get this darn script working.

Thanks a bunch,
Marcel
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

duh.. heh...forgetful me.
Post Reply