• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to secondary sidebar

CodingStatus

- Level Up Your Status with Coding Expertise!

  • Tutorials
    • React Js Tutorials
    • JavaScript Tutorials
    • PHP Tutorials
    • HTML Tutorials
    • SQL Tutorials
    • Laravel Tutorials
  • Snippets
    • React Js Code Snippets
    • Ajax Code Snippets
    • Node.js Code Snippets
    • JavaScript Code Snippets
    • jQuery Code Snippets
    • SQL Code Snippets
  • Programs
    • PHP Program
    • Python Programs
    • Java Programs
  • How To
    • React Js How To
    • Node Js How To
    • Ajax How To
    • PHP How To
    • jQuery How To
    • JavaScript How to
  • Scripts
    • PHP Script
    • Js Script
    • React Js Script
    • JQuery Script
  • Projects
    • PHP Project
  • Interview Questions
  • Guide
  • API Integration
  • Installation

Node.js MySQL Delete – How to delete data Using Node.js and MySQL

October 31, 2020 By Md Nurullah

Node.js MySQL Delete: This tutorial will help you to delete the data of the MYSQL table row. it will be very useful for you. Even you will get the best & easy Node.js MySQL Delete Query with a Simple example. So, Read all the next steps and use the given concept in your project.

node.js mysql delete data

Table of Contents

  • Node.js MySQL Delete Query with Example
    • 1. Install Express Application
    • 2. Connect Node.js App to MySQL
    • 3. Create Delete Link Button
    • 4. Create Routes to Delete Data
    • 5. Load Route File into Root File
    • 6. Run Node.js Code to update data 
    • My Suggestion

Node.js MySQL Delete Query with Example

Before start coding to delete data, you must insert data into MYSQL Database and display data from the MySQL database. Otherwise, you may not understand this tutorial properly.

1. Install Express Application

You have to Install Basic Express Application and create the following folders and files

myapp/
  |__routes/
  |    |__users.js
  |__views/
  |    |__user-list.ejs
  |__app.js
  |__database.js
  |

Note – If you have already installed the express application then you need not do it again

2. Connect Node.js App to MySQL

You must make sure the MySQL module is installed and included in the database connection file. After that, write the connection script in the database.js

File Name – database.js

var mysql = require('mysql');
var conn = mysql.createConnection({
  host: 'localhost', // Replace with your host name
  user: 'root',      // Replace with your database username
  password: '',      // Replace with your database password
  database: 'nodeapp' // // Replace with your database Name
}); 
conn.connect(function(err) {
  if (err) throw err;
  console.log('Database is connected successfully !');
});
module.exports = conn;

3. Create Delete Link Button

You have to create a delete button link for each record in the HTML table. So, to do it, you have to display all the records in an HTML table and just add the following line to delete data in user-list.ejs  –

<td><a href="/users/delete/<%=data.id%>">Delete</a></td>

4. Create Routes to Delete Data

Configure the following points to create routes for deleting data –

  • Include database connection file database.js
  • Create a route /delete/:id with the GET method to display data in the HTML form.
  • Write SQL query to delete data.
  • If the query returns true then redirect to /users/user-list.

File Name – users.js

router.get('/delete/:id', function(req, res, next) {
  var id= req.params.id;
    var sql = 'DELETE FROM users WHERE id = ?';
    db.query(sql, [id], function (err, data) {
    if (err) throw err;
    console.log(data.affectedRows + " record(s) updated");
  });
  res.redirect('/users/user-list');
  
});

Also, make sure that Database Name is nodeapp and Table Name is users.  If you want to implement this script to your own database & table name of your project then you can do it. But I suggest you should execute it as it is given if you are a learner. after that, you can use it in your project.

5. Load Route File into Root File

You must load the route file users.js into the root file app.js. If users.js already existed then you need not create it again.

File Name – app.js

var usersRouter = require('./routes/users');
;
app.use('/users', usersRouter);

Note – If the above code already exists in the app.js  then you can skip this step.

6. Run Node.js Code to update data 

To Run Node.js code for deleting data, you will have to do the following things –

  • First of all, Start your Node.js server using npm start command
  • Enter the following URL into your web browser to display records in an HTML table
http://localhost:3000/users/user-list

When you click the delete link button, data will be deleted successfully based on the passing id from the delete link button.

You have learned to delete simple data from the database. Now you can implement the concept of this tutorial to any kind of table data.

My Suggestion

Dear developers, I hope you have understood the above script, Now you are able to delete data using Node.js & MySQL.Even you can delete another record by using the above steps.

If you have any questions or suggestions regarding Node.js. You can directly ask me through the below comment box.

Filed Under: Node Js How To

Hello Developer, Welcome to my Blog. I'm Md Nurullah, a software engineer with 5+ years in full-stack web development. As the founder of CodingStatus.com, I'm passionate about sharing coding knowledge and collaborating for a brighter future. Let's connect and code together!

Primary Sidebar

Secondary Sidebar

Node Js How To,
  • How to Insert Form Data Into the Table Using Node.js and MySQL
  • How to display Data from MySQL database table in Node.js
  • How to Download and Install Node.js on Windows with NPM
  • How to Display Image From MySQL Database in Node.js
  • How to Store Image in MySQL Database Using Node.js
  • Node.js MySQL Delete – How to delete data Using Node.js and MySQL
  • How to Update Data Using Node.js and MySQL
  • Delete MongoDB Data using Mongoose and Node.js
  • Update MongoDB Data Using Mongoose and Node.js
  • How to Insert Data into MongoDB Using Mongoose and Node.js
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved