Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL insert error : ER_BAD_FIELD_ERROR: Unknown column '2525' in 'field list'

Tags:

sql

mysql

var convID = 2525;
var contactUsername = 'blabla';
var userId = 100;
var contactId = 200;

var sql = "INSERT INTO contacts (FK_OWNERID, FK_USERID, FC_CONTACTNAME, `CONVERSATION_ID`)
                     VALUES (" + mysql.escape(userId) + "," + mysql.escape(contactId) + "," + mysql.escape(contactUsername) + "," + convID + "),
                     (" + mysql.escape(contactId) + "," + mysql.escape(userId) + "," + mysql.escape(username) + "," + convID + ")";

`Error: ER_BAD_FIELD_ERROR: Unknown column '2525' in 'field list' Fields are varchar(32) for both CONVERSATION_ID and FC_CONTACTNAME and ints for the 2 others.

What's wrong with this query? I even put the backticks around CONVERSATION_ID and it thinks the convID variable is a column...

Edit:

If my query is just that, then it works:

        var sql = "INSERT INTO contacts (`FK_OWNERID`, `FK_USERID`, `FC_CONTACTNAME`)
                 VALUES (" + mysql.escape(userId) + "," + mysql.escape(contactId) + "," + mysql.escape(contactUsername) + "),
                 (" + mysql.escape(contactId) + "," + mysql.escape(userId) + "," + mysql.escape(username) + ")";
like image 328
Dan P. Avatar asked Apr 11 '14 19:04

Dan P.


2 Answers

make

" + convID + "

to

'" + convID + "'

Most likely its a string and the data type is varchar so u need to enclose within single quote.

like image 140
Abhik Chakraborty Avatar answered Oct 05 '22 05:10

Abhik Chakraborty


I had a similar problem myself, I also didn't have all of my column names specified correctly. here is the code that finally worked.

var queryString = "INSERT INTO " + table + " (columnText, columnBool) VALUES ('" + val + "', FALSE);"
like image 33
Brian Russell Avatar answered Oct 05 '22 05:10

Brian Russell