0

Hey Guys I'm getting a syntax error with my SQL on my PHP page.

Basically what my page does is that it simply inserts data from a form and inserts into the database.

This is my code and my error:

Problem with query.

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 '', '', '', '', '', '')' at line 2

$sql = "INSERT INTO user (username, password, gname, mname, surname, address, state, postcode, tel)
VALUES ( '$usernamestr', '$passwordstr','$firstnamestr', '$middlenamestr’, '$familynamestr', '$addressstr', '$statestr', '$postcodestr', '$phonestr');";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

I thought this was the way to do it? which is absolutely weird.

4
  • 1
    xkcd.com/327 -- Learn about PDO and all your problems will be solved. Commented May 28, 2014 at 14:56
  • Clearly the variables don't have the expected values, which is great, because it's preventing passwords from being stored as plaintext. Commented May 28, 2014 at 14:56
  • Looks like the variables are empty. Commented May 28, 2014 at 14:56
  • Side note: You should escape the variables, before using them in the query. de1.php.net/mysql_real_escape_string or if you choose MYSQLi de1.php.net/manual/en/mysqli.real-escape-string.php Commented May 28, 2014 at 14:59

1 Answer 1

10

You have a funky quote:

VALUES ( '$usernamestr', '$passwordstr','$firstnamestr', '$middlenamestr’, '$familynamestr', 
                                                                       ^^^^
                                                                       HERE

It is a and needs to be a ' (as the syntax highlighter shows in my answer but not in your question).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks dude, I really cant believe i missed that. Looked exactly like a single quote to me.
I had to re-read it several times to spot it as it definitely does not stand out. If your query wasn't otherwise correct I wouldn't have looked at it quite so hard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.