0

Im trying to use insert function and Im getting all the time an error :( Im trying to insert into user table a name of the user

My function:

 function insert(tableName, toField, value){
    connection.connect();
    var queryString = "insert into "+tableName+" ("+toField+") 
    select "+connection.escape(value)+"";    

    connection.query(queryString, function(err) {
        if (err) throw err;
    });     
}

and in the main code:

var name ="Anna";
Helpers.insert('user', 'name', name);

Everytime I get an error:

Uncaught exception: Error: ER_PARSE_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 ''user' ('name') select 'Anna'' at line 1
Error: ER_PARSE_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 ''user' ('name') select 'Anna'' at line 1

3 Answers 3

1

I made it :) Thank for your help!!

 function insert(tableName, toField, value){
    connection.connect();
    var queryString = "INSERT INTO  "+tableName+" ("+toField+") VALUES ("+connection.escape(value)+")";    

    connection.query(queryString, function(err) {
        if (err) throw err;
    });     
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add Values() which is missing,

var queryString = "insert into "
    +connection.escape(tableName)+" ("+connection.escape(toField)+")
    Values (" + connection.escape(value)+")";

1 Comment

The same ;( Error: ER_PARSE_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 ''user' ('name')values 'Anna'' at line 1
0
    var queryString = 
        "INSERT INTO " + connection.escapeId(tableName)

You could use connection.escapeId to escape an identifier (as opposed to a value).

3 Comments

still the same ;( Uncaught exception: Error: ER_PARSE_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 'Anna' at line 1
@AnnaK could you do var query = connection.query(...) and then console.log(query.sql)?
insert into user (name) values Anna That I get from console.log(queryString). Is it strange. Isn't? Because it looks ok

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.