TO DELETE A USER (DELETE /user/:id)

 INDEX.JS


//TO DELETE
app.delete("/user/:id", (req, res) => {
  let { id } = req.params;
  let q = `DELETE FROM user WHERE id='${id}'`;
  try {
    connection.query(q, (err, result) => {
      console.log(result);
        if(err) throw err;
        res.redirect("/users");
    });
  } catch (err) {
    res.send("error occured");
  }
});

SHOW.EJS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>show users</title>
    <style>
    table,
    th,
    td{
        border: 1px solid black;
    }
    </style>
</head>

<body>
    <table>
    <tr>
        <th>Id</th>
        <th>username</th>
        <th>Email</th>
<% for(user of users){ %>
<tr>
    <td><%= user.id %></td>
    <td><%= user.username %></td>
    <td><%= user.EMAIL %></td>
    <td>
        <form method="get" action="/user/<%= user.id %>/edit">
            <button>Edit username</button>
        </form>
    </td>
    <td>
        <form method="post" action="/user/<%=user.id%>?_method=DELETE">
            <button>DELETE</button></form>

    </td>
</tr>
<%}%>
    </tr>
    <tr>

    </tr>
</table>
</body>
</html>


Comments

Popular posts from this blog

DATABASE RELATIONSHIPS

ROUTING (GET /) home route

Query Strings