tags:

views:

43

answers:

4

So this is probably a dumb beginner question, but I've been looking at it and can't figure it out. A bit of background: just practicing making a web app, a form on page 1 takes in some values from the user, posts them to the next page which contains the code to connect to the DB and populate the relevant tables.

I establish the DB connection successfully, here's the code that contains the query:

$conn->query("SET NAMES 'utf9'");         

$query_str = "INSERT INTO 'qa'.'users' ('id', 'user_name','password' ,'email' ,'dob' ,'sx') VALUES (NULL, $username, $password, $email, $dob, $sx);";

$result = @$conn->query($query_str);

Here's the error that is returned:Insert query failed: 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 ''qa'.'users' ('id', 'user_name' ,'password' ,'email' ,'dob' ,'s' at line 1

Thanks in advance!

A: 

Unless it's changed since I did MySQL in PHP, escape your db/column/table names with backticks (`), not apostrophes (').

JustABill
if i use backticks, i get a Unknown column ... in 'field list' error
Olegious
Can you give the specific error, please?
JustABill
you know what, I tried it again, and it worked in myphpadmin. thanks!
Olegious
the specific error is: "Insert query failed: Unknown column 'olegs' in 'field list'" "olegs" is one of the inputs that i attach to the $username variable, so not sure why it is telling me about a "olegs" column, the column should be "user_name"
Olegious
i've narrowed the issue down to the way i use the variable names in the sql statement, i can take it from here, thank you.
Olegious
+1  A: 

A good general trouble-shooting technique is to make the query work via another interface to the database. For example, phpMyAdmin. If it works there, you have some confidence going forward. or you may find how to fix your SQL. (phpMyAdmin is handy because it will convert your SQL into a ready-made string for PHP.)

Smandoli
good idea- this is the error i get through phpmyadmin: 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 ''qa'.'users' ('id', 'user_name','password' ,'email' ,'dob' ,'sx') VALUES (NULL, ' at line 1basically the same thing, right?
Olegious
Heh heh ... not terribly helpful in itself, I suppose. But making repeated attempts is a lot easier than doing it through HTTP!
Smandoli
A: 

You need to escape your column names with a backtick (`) instead of (')

You also need to properly escape the actual values you are inserting as well (use a single quote)

webdestroya
A: 

OMG not a single right answer

$query_str = "
INSERT INTO `qa`.`users` (`id`, `user_name`,`password` ,`email` ,`dob` ,`sx`) 
VALUES (NULL, '$username', '$password', '$email', '$dob', '$sx')";

identifiers being quoted with backticks, while strings being quoted with apostrophes!

and I hope you have passed all your variables through mysql_real_escape string BEFORE putting it into query, i.e.:

$username = mysql_real_escape string($username);

and so on

Col. Shrapnel