user13405446
user13405446

Reputation: 23

Node.js Mysql Insert Object with On Dublicate

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;

Upvotes: 0

Views: 1653

Answers (1)

Ravi
Ravi

Reputation: 2281

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;
    });

Upvotes: 2

Related Questions