Speedysnail6
Speedysnail6

Reputation: 114

MySQL Syntax Error insert into

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

Upvotes: 2

Views: 891

Answers (5)

juan
juan

Reputation: 179

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

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

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172618

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)

Upvotes: 1

amaster
amaster

Reputation: 2163

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 ,

Upvotes: 0

Kermit
Kermit

Reputation: 34062

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.

Upvotes: 1

sealz
sealz

Reputation: 5408

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)

Upvotes: 1

Related Questions