I want to delete some rows from MySQL db using checkboxes from my ejs view to routes with node.js and React app. I can delete one row, but how can i pass and array of parameters?
This is my router delete-func:
router.get("/delete/:id", function (request, res, next) {
var id = request.params.id;
const sql = `DELETE FROM users WHERE id = "${id}" `;
This is "delete button html code" with id for deleting one row from ejs file:
<a href="/users/delete/<%=data.id%>" class="btn btn-danger btn-sm">Delete</a>
I can delete one row, but I can’t delete more at once.
2
Answers
First of all, you should use DELETE instead of GET method to delete single row in your router.
You can use POST method to send an array of inputs to the server. So just need put a checkbox for each rows which name is "shouldDelete[]" or another name you like it.
It’s important that using the brackets to send an array of inputs to the server.
And then you should use a
<form>
to send your POST request.Client:
Server:
Notice: Observe safety precautions when querying the database.