2

This is my SQL query

INSERT INTO users (Username, Password, Email, Admin)
    VALUES ($username, $encryptedpass, $email, 0)

I get this 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 ' , , 0)' at line 2

Could you please explain where I am messing up, and how to fix it

2
  • You're missing some of these : ' Commented Aug 28, 2013 at 17:55
  • Yeah, sorry about that Commented Aug 28, 2013 at 18:01

5 Answers 5

1

You need to wrap your values in single quotes (assuming Admin is an integer):

INSERT INTO users (Username, Password, Email, Admin)
    VALUES ('$username', '$encryptedpass', '$email', 0)

For best practice, you should encase all system keywords in backticks:

INSERT INTO `users` (`Username`, `Password`, `Email`, `Admin`)
    VALUES ('$username', '$encryptedpass', '$email', 0)

If you're using PHP to send this data, you should be using prepared statements using MySQLi or PDO.

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

1 Comment

Thanks! Sorry if that was a simple question, I just am new to MySQL. And, yes I am using PHP.
1

You need to enclose your variables with ' to correctly format your variables. Integer fields do not require enclosure.

INSERT INTO users (Username, Password, Email, Admin)
VALUES ('$username', '$encryptedpass', '$email', 0)

2 Comments

I appreciate your response, but I needed them all to be surrounded by 's
@speedysnail6 if the 0 needed to be enclosed then that field is not expecting an integer.
1

Try this:-

INSERT INTO users (Username, Password, Email, Admin)
VALUES ('$username', '$encryptedpass', '$email', '0')

or

 INSERT INTO users (Username, Password, Email, Admin)
VALUES ('$username', '$encryptedpass', '$email', 0)

1 Comment

Thanks! Sorry if that was a simple question, I just am new to MySQL. And, yes I am using PHP.
0
INSERT INTO users (Username, Password, Email, Admin)
    VALUES ($username, $encryptedpass, $email, 0)

Should be

INSERT INTO users (Username, Password, Email, Admin)
    VALUES ('$username', '$encryptedpass', '$email', '0')

The problem was that an empty string was not being inserted it was being read as

INSERT INTO users (Username, Password, Email, Admin)
    VALUES (, , , 0)

When all three variables were empty strings instead of

INSERT INTO users (Username, Password, Email, Admin)
    VALUES ('', '', '', '0')

The empty string '' tells MySQL to empty the empty string instead of go to the next value with ,

2 Comments

Thanks! Sorry if that was a simple question, I just am new to MySQL. And, yes I am using PHP.
@Speedysnail6 no problem, we all started somewhere and got stomped with simple stuff like this along the way. Just remember to keep the manuals at a handy shortcut php.net and MySQL And help out by accepting what you think is the best answer with the green checkmark and any helpful answers with the ^
0

Are you using PHP? Text date must go between ''

INSERT INTO users (Username, Password, Email, Admin) VALUES ('$username', '$encryptedpass', '$email', 0)

Comments

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.