Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Node.js MySQL Insert Records

Node.js MySQL Insert Records

Posted on October 20, 2025October 20, 2025 By Rishabh saini No Comments on Node.js MySQL Insert Records

Node.js MySQL Insert Records

The INSERT INTO statement in MySQL is used to add new records into a database table. When working with Node.js, you can use the mysql module to easily insert data into a MySQL database. Let’s look at how to insert single and multiple records using Node.js.

Data Science Tutorial:–Click Here

Insert a Single Record

Let’s insert a single record into the employees table.

Step 1:
Create a JavaScript file named insert.js inside the DBexample folder and add the following code:

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: "12345",  
  database: "updategadh"  
});  

con.connect(function(err) {  
  if (err) throw err;  
  console.log("Connected!");  
  var sql = "INSERT INTO employees (id, name, age, city) VALUES ('1', 'Ajeet Kumar', '27', 'Allahabad')";  
  con.query(sql, function (err, result) {  
    if (err) throw err;  
    console.log("1 record inserted");  
  });  
});  

Introduction to Applied AI:–Click Here

Step 2:
Open the command terminal and run:

node insert.js

After successful execution, the terminal will display:

Connected!  
1 record inserted  

Step 3:
Verify the inserted record using the following SQL query:

Download New Real Time Projects :–Click here

SELECT * FROM employees;

Insert Multiple Records

You can also insert multiple records at once using the same approach.

Step 1:
Create another file named insertall.js in the DBexample folder and add this code:

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: "12345",  
  database: "updategadh"  
});  

con.connect(function(err) {  
  if (err) throw err;  
  console.log("Connected!");  
  var sql = "INSERT INTO employees (id, name, age, city) VALUES ?";  
  var values = [  
    ['2', 'Bharat Kumar', '25', 'Mumbai'],  
    ['3', 'John Cena', '35', 'Las Vegas'],  
    ['4', 'Ryan Cook', '15', 'CA']  
  ];  
  con.query(sql, [values], function (err, result) {  
    if (err) throw err;  
    console.log("Number of records inserted: " + result.affectedRows);  
  });  
});  

Step 2:

SQL Tutorial :–Click Here
Run the script using:

node insertall.js

Expected output:

Connected!  
Number of records inserted: 3  

Step 3:
Check the inserted records using:

SELECT * FROM employees;

The Result Object

Complete Python Course with Advance topics:-Click Here

When you execute an INSERT query in Node.js, the result object returned by MySQL provides details about the operation.
For example, it includes the number of affected rows, insert ID (if applicable), and server status information.

A simplified result object looks like this:

{
  "fieldCount": 0,
  "affectedRows": 1,
  "insertId": 1,
  "serverStatus": 2,
  "warningCount": 0,
  "message": "",
  "protocol41": true,
  "changedRows": 0
}

This allows you to verify whether your insertion was successful and how many records were affected.

Machine Learning Tutorial:–Click Here
Complete Advance AI topics:- CLICK HERE
Deep Learning Tutorial:– Click Here


nodejs mysql insert, mysql insert query nodejs, insert multiple records in mysql using node.js fails, node.js mysql insert, node.js mysql insert data, node.js mysql insert statement, insert record into mysql database, how do i avoid insert duplicate records in node mysql?, node js mysql insert data, node js mysql insert into, node js mysql insert variables, insert form data into mysql using node.js, insert query in node js mysql, insert data in mysql node js, nodejs mysql select, nodejs mysql how to insert data dynamically in mysql using node js node js insert data into mysql how to insert data in mongodb using node js using mongoose node js mysql query return result mysql2 insert multiple rows how to insert data in database using javascript in php how to connect html form to mysql database using node js express js mysql node js mysql insert records w3schools node js mysql insert records example

    Post Views: 244
    Node.js Tutorial Tags:how do i avoid insert duplicate records in node mysql?, insert data in mysql node js, insert form data into mysql using node.js, insert multiple records in mysql using node.js fails, insert query in node js mysql, insert record into mysql database, mysql insert query nodejs, node js mysql insert data, node js mysql insert into, node js mysql insert variables, node.js mysql insert, node.js mysql insert data, node.js mysql insert statement, nodejs mysql, nodejs mysql insert, nodejs mysql select

    Post navigation

    Previous Post: Node.js MySQL Create Table
    Next Post: E-commerce Website using MERN

    More Related Articles

    Node.js File System Node.js File System (FS) Node.js Tutorial
    Node.js Web Module Node.js Web Module Node.js Tutorial
    Node.js MongoDB Create Collection Node.js MongoDB Create Collection Node.js Tutorial

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may also like

    1. Install Nodejs on Windows
    2. Node.js Command Line Options
    3. Node.js Assertion Testing
    4. Node.js MySQL Delete Records
    5. Node.js Create Connection with MongoDB
    6. Node.js MongoDB Filter Query

    Most Viewed Posts

    1. Top Large Language Models in 2025
    2. Online Shopping System using PHP, MySQL with Free Source Code
    3. login form in php and mysql , Step-by-Step with Free Source Code
    4. Flipkart Clone using PHP And MYSQL Free Source Code
    5. News Portal Project in PHP and MySql Free Source Code
    6. User Login & Registration System Using PHP and MySQL Free Code
    7. Top 10 Final Year Project Ideas in Python
    8. Online Bike Rental Management System Using PHP and MySQL
    9. E learning Website in php with Free source code
    10. E-Commerce Website Project in Java Servlets (JSP)
    • AI
    • ASP.NET
    • Blockchain
    • ChatCPT
    • code Snippets
    • Collage Projects
    • Data Science Project
    • Data Science Tutorial
    • DBMS Tutorial
    • Deep Learning Tutorial
    • Final Year Projects
    • Free Projects
    • How to
    • html
    • Interview Question
    • Java Notes
    • Java Project
    • Java Script Notes
    • JAVASCRIPT
    • Javascript Project
    • JSP JAVA(J2EE)
    • Machine Learning Project
    • Machine Learning Tutorial
    • MySQL Tutorial
    • Node.js Tutorial
    • PHP Project
    • Portfolio
    • Python
    • Python Interview Question
    • Python Projects
    • PythonFreeProject
    • React Free Project
    • React Projects
    • Spring boot
    • SQL Tutorial
    • TOP 10
    • Uncategorized
    • Online Examination System in PHP with Source Code
    • AI Chatbot for College and Hospital
    • Job Portal Web Application in PHP MySQL
    • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
    • Online Job Portal System in JSP Servlet MySQL

    Most Viewed Posts

    • Top Large Language Models in 2025 (8,614)
    • Online Shopping System using PHP, MySQL with Free Source Code (5,215)
    • login form in php and mysql , Step-by-Step with Free Source Code (4,867)

    Copyright © 2026 UpdateGadh.

    Powered by PressBook Green WordPress theme