Node.js MySQL Insert Into Table
Node.js Tutorial » Node.js MySQL Insert Into Table
How to Insert Data into MySQL Database Using Node JS?
In this chapter you will learn how to insert a new record intoMySQL table from node.js application.
Use these steps to ionsert row into a table from nodejs:
1. First connect to MySQL db server.
2. Second Let’s, create a node_mysql_insert_tbl_test.js file and put the following code in it –
3. Close the DB connection.
Node.js MySql insert into Table
To fill a table in MySQL, use the "INSERT INTO" statement.
Let’s insert row into MySQL Table named “Users” under “my_db_name”.
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "Users;
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address, email) VALUES ('Stwart A', 'Highways 27', 'stwarta@agernic.com')";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("row inserted");
});
});
Save the code above in a file named "demo_test_db_insert.js", and run the file:
Related subjects:
node.js create database
nodejs create table
node.js Send Email
upload files
node js insert into mysql, data into mongodb, into postgresql, json to mysql, into table, array, to sql server, query mysql, into, query in loop, insert variables, insert multiple rows, insert return id,
how to insert data from a form into database using node js
node js mysql insert variables
insert query in node js mssql
nodejs mysql insert multiple rows
nodejs mysql insert return id
how to insert data from a form into database using node js
node js mysql insert multiple rows json
how to return a value from a mysql select query in node js
Insert Multiple Records
To insert more record, you have to make an array containing the values, and insert a question mark in the sql
We create a demo_test_multi_rows_db_insert.js file and put the following code in it
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address, email) VALUES ?";
var values = [
['KeithN', '28 Amb', 'KeithN@agernic.com'],
['Alex Mar', '14 Cyber', 'Alex-Mar@agernic.com'],
['Murphy Jo', '15 Guion', 'Murphy-Jo@agernic.com'],
['Amanda Key', '35 Shefild', 'Amanda-Key@agernic.com'],
['Jason Rak', '31 Nako', 'Jason-Rak@agernic.com'],
];
conn.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Ok | MySQL Multiple Record Inserted");
console.log("No. of records inserted- " + result.affectedRows);
});
});
Save the code above in a file called "demo_test_multi_rows_db_insert.js", and run the file:
Get Inserted ID
Insert a record in the "users" table, and return the ID.
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address, email) VALUES ('Stwart A', 'Highways 27', 'stwarta@agernic.com')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted, ID: " + result.insertId);
});
});
Save the code above in a file called "demo_test_multi_rows_db_insert.js", and run the file:
node js insert into mysql, data into mongodb, into postgresql, json to mysql, into table, array, to sql server, query mysql, into, query in loop, insert variables, insert multiple rows, insert return id,
how to insert data from a form into database using node js
Node.js MySQL Insert Into Table - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.