/    Sign up×
Community /Pin to ProfileBookmark

Error in simple MySQL code

Hi, not sure what I’ve done wrong here…

I have the following code

[CODE]
mysql_query(“INSERT INTO bandwusers (firstName,lastName,gender,age,email,dateRegistered,postalAddress,username,
password,newsletter) VALUES
($firstName,$lastName,$gender,$age,$email,$dateRegistered,$postalAddress,$username,$password,$newsletter)”) or die(mysql_error());
[/CODE]

However I’m getting error messages saying:

“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 ‘,Male,,,2007-8-7,,,hhyrl1l,Yes)’ at line 1”

[Note, the values it refers to are the values I’m trying to insert into the db]

This is such a simple little prob, but can’t think what I’ve done wrong… can anyone help? Thanks so so much!! ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 07.2007 — Firstly, string literals in SQL must be quoted just as they are in MySQL. The variables you are passing to MySQL in the VALUES list of the query will be converted to their actual values when they are sent to MySQL, so they will need to be quoted if they are not numeric values. Also, you can use the following pattern to help you debug, by showing you the actual query string as it was sent to MySQL if it fails:
[code=php]
$query = "INSERT INTO bandwusers (firstName, lastName, gender, age, email,
dateRegistered, postalAddress, username, password, newsletter)
VALUES ('$firstName', '$lastName', '$gender', $age, '$email', '$dateRegistered',
'$postalAddress', '$username', '$password', '$newsletter')";
$result = mysql_query($query) or die("Insert failed ($query): " . mysql_error());
[/code]
Copy linkTweet thisAlerts:
@sloLearnerauthorAug 07.2007 — Thanks for the quick reply NogDog, and the debugging advice. Bizarrely I'm getting the same kind of error messages:

Insert failed (INSERT INTO bandwusers (firstName, lastName, gender, age, email,dateRegistered, postalAddress, username, password, newsletter) VALUES ('adsf', 'kl', 'Male', , 'jk', '2007-8-7','jk', 'kj', 'm7klcyf', 'Yes')): 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 ' 'jk', '2007-8-7','jk', 'kj', 'm7klcyf', 'Yes')' at line 1

I'm truly confused as to what's gone wrong here...
Copy linkTweet thisAlerts:
@NogDogAug 07.2007 — Looks like $age had no value. Either put quotes around it, too, if a null value is OK, or check your code to figure out why it's not being set if it should have been.
Copy linkTweet thisAlerts:
@sloLearnerauthorAug 07.2007 — Phew... yes, that's it. I think I'm going to start investing some time in debugging techniques, as I seem to have a habit of missing simple things like these. Thanks again for all your help NogDog! ?
×

Success!

Help @sloLearner spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...