Node.js MySQL Insert Into

πŸš€ Node.js MySQL – INSERT INTO

To insert data into MySQL using Node.js:

  1. Connect to the database

  2. Use an SQL INSERT statement

  3. Execute using connection.query() or connection.execute() (prepared statement)


βœ… 1. Install mysql2

npm install mysql2

βœ… 2. Connect to MySQL Database

Example: Using database mydb


 


βœ… 3. Insert a Single Record

insert_one.js


 


πŸ“ Output Example

Connected!
Record Inserted! Insert ID: 1

βœ… 4. Insert Multiple Records


 


βœ… 5. Insert with Prepared Statements (Recommended)

Protects from SQL injection.



 


βœ… 6. Insert Using Async/Await (Best Way)

(Using mysql2 promise API)

insert_async.js


 


πŸ“Œ 7. Insert with Currents Timestamp Auto

If your table uses:



 

Then you don’t need to insert the date β€” it fills automatically.


🎯 8. Checking Inserted Data

After insert, you can check:



 

Or create a small Node query to fetch data.

You may also like...