2

New to node.js and learning how to use mysql with node. I have a form.html that has two buttons to input a department_no and a department_name. I am succecfully able to insert into mysql database but I have no clue on how to delete a specific dept_no. What I want to be able to do is enter a dept_no and the dept_name and then DELETE from my database based on the dept_no. Also I want to be able to check what the user enters to make sure that it is a valid dept_no and dept_name. Any ideas on how to get started or what to look into to be able to do this would be extremely helpful. I will paste my node.js and form.html down below.

node.js

// DELETE BELOW
app.delete("/deleteEmp/", (req, res) => {
  console.log("Department number is " + req.body.department_no);
  console.log("Department name is " + req.body.department_name);

  const deptNumber = req.body.department_no;
  const deptName = req.body.department_name;

  const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "employees"
  });

  const queryString = "DELETE departments WHERE dept_no = ?";
  connection.query(queryString, [department_no], (err, results, fields) => {
    if (err) {
      console.log("Could not delete" + err);
      res.sendStatus(500);
      return;
    }
    console.log("Deleted department_no with dept_name");
    res.end();
  });
});

form.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>SQLFORM</title>
    <style>
      #space {
        margin-top: 20px;
      }
    </style>
  </head>
  <body>
    <div>Fill Form Below</div>
    <hr />
    <div id="space">
      <form action="/deleteEmp" method="DELETE">
        <input placeholder="Enter Department Number" name="department_no" />
        <input placeholder="Enter Department Name" name="department_name" />
        <button>Submit</button>
      </form>
    </div>
  </body>
</html>
4
  • Try const queryString = "DELETE FROM departments WHERE dept_no = ?"; instead. Commented Nov 18, 2018 at 21:38
  • but how can I use a form and get the info passed into the form and delete based on that in my database Commented Nov 18, 2018 at 21:44
  • this post doesn't explain how to delete from a mysql database using express though @Barns Commented Nov 18, 2018 at 22:20
  • 1
    You can use sequelizejs (ORM) docs.sequelizejs.com for performing node database operations. It will make easy for you to perform CRUD operations. Commented Nov 19, 2018 at 9:32

2 Answers 2

1

HTML forms do not support DELETE requests in general. Node apps can use a work-around like this one - https://github.com/expressjs/method-override

Sign up to request clarification or add additional context in comments.

Comments

1

Another alternative is to perform an AJAX request that doesn't have restrictions on allowed methods. A simplified example:

document.querySelector('#space form').addEventListener('submit', (event) => {
  event.preventDefault();

  let formData = new FormData();
  formData.append('department_no', document.querySelector("input[name='department_no']").value);
  formData.append('department_name', document.querySelector("input[name='department_name']").value);

  fetch('https://yoururl.com/deleteEmp', {
    method: 'DELETE',
    headers: {
      "Content-type": "application/x-form-urlencoded"
    },
    body: formData
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.