0

How this query would be correct?

let newUser = {
    user_id:'123456',
    user_name:'someone'
    }

con.query('INSERT INTO users SET ? ON DUPLICATE KEY UPDATE ?', newUser, (err,rows) => {
        if(err) throw err;
    });

At the moment i get a syntax error after UPDATE

EDIT

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 '?' at line 1


 sql: "INSERT INTO users SET `user_id` = '', `user_name` = '' ON DUPLICATE KEY UPDATE ?"

TABLE STRUCTURE

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_Id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `user_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`,`user_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=86 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
2
  • 1
    share the complete error log and the query generates Commented Oct 4, 2020 at 7:37
  • Edited values are empty cause i deleted the real ones here Commented Oct 4, 2020 at 7:58

1 Answer 1

2

The number of question marks should be equal to the size of the array. You have to pass 2 params

let newUser = {
    user_id:'123456',
    user_name:'someone'
    }

con.query('INSERT INTO users SET ? ON DUPLICATE KEY UPDATE ?', [newUser, newUser], (err,rows) => {
        if(err) throw err;
    });
Sign up to request clarification or add additional context in comments.

9 Comments

con.query('INSERT INTO users SET ? ON DUPLICATE KEY UPDATE ?', user, user (err,rows) => { ^^^^^^^^^^^^^^ SyntaxError: Malformed arrow function parameter list
[newUser, newUser] is an array parameter con.query has 3 params 1. query, 2. input values, 3. callback
Okey got it so far :) but when i now change the user_name it makes a new entry. I only want having the user_id onces in my table
And it makes now everytime a new entry ... still when it has same values
check with the primary key and unique key indexes ON DUPLICATE which check for those keys
|

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.