S
S
Sedbol2019-12-11 00:25:59
Node.js
Sedbol, 2019-12-11 00:25:59

How to insert a record or update?

How to do INSERT INTO ON DUPLICATE KEY UPDATE ?

function userWin(val) {
    return new Promise(function (resolve, reject) {       
        const win="UPDATE `user` SET `monets`= monets+? ,`exp`=exp+? WHERE `uid`=?";
        const par = [val.gold,val.xp,val.user];
        connection.query(win, par,
            function(err, results) {
                if (err) reject(err);
                resolve(results);
            });
        connection.end();
        //console.info(val)
    })
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2019-12-11
@Sedbol

Some strange request) Rebus for a drug addict)

function userWin(val) {

    return new Promise(function (resolve, reject) {  

    	const par = [val.gold,val.xp,val.user];   

        const win = `UPDATE user SET monets = ${par[0]},exp = ${par[1]} WHERE uid = ${par[2]}`;

        connection.query(win, (err, results) => {

                if (err) reject(err);

                resolve(results);

        });

        connection.end();

    }).then(r => {
    	console.log(r)
    })

}

And it would be correct like this:
function userWin(val) {

    return new Promise(function (resolve, reject) {  

    	const par = [val.gold,val.xp,val.user];   

    	let monets = par[0],
    		exp = par[1];

        const win = `UPDATE user SET ? WHERE uid = ${par[2]}`;

        connection.query(win, {monets,exp}, (err, results) => {

                if (err) reject(err);

                resolve(results);

        });

        connection.end();

    }).then(r => {
    	console.log(r)
    })

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question