Node.js MySQL: Update Records
The MySQL UPDATE statement modifies existing records. This tutorial shows you how to update data in a MySQL table from Node.js using the mysql package.
Node.js Tutorial:-
SQL Tutorial:-
Example: Update City
// update.js
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "updategadh"
});
con.connect(function(err) {
if (err) throw err;
const sql = "UPDATE employees SET city = 'Delhi' WHERE city = 'Allahabad'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s) updated");
});
});
Run It
node update.js
# Output: 1 record(s) updated
Parameterized UPDATE (Safer)
Always use parameters to avoid SQL injection:
const sql = "UPDATE employees SET city = ? WHERE city = ?";
const values = ['Delhi', 'Allahabad'];
con.query(sql, values, function (err, result) {
if (err) throw err;
console.log(`${result.affectedRows} updated`);
});
UPDATE Multiple Columns
const sql = "UPDATE employees SET city = ?, salary = ? WHERE id = ?";
con.query(sql, ['Mumbai', 60000, 1], (err, result) => {
if (err) throw err;
console.log(`${result.affectedRows} updated`);
});
Verify the Result
The result.affectedRows property tells you how many rows changed. Always log it to confirm your UPDATE worked as intended.
Download New Real Time Projects:- Click here
Complete Advance AI topics:-
Conclusion
Updating MySQL records from Node.js is straightforward. Use parameterized queries for safety and always check affectedRows for confirmation. For more tutorials, stay tuned to .
nodejs mysql update
mysql update nodejs
nodejs mysql update data
mysql update query nodejs
node.js mysql update
node js mysql update query
node mysql update
node js mysql update records w3schools