mysql - 如何删除选定的行mysql express.js

标签 mysql node.js express ejs

我正在尝试从数据库中删除选定的行。我已经实现了将数据添加到数据库。我正在尝试执行与将元素添加到数据库时相同的操作,但它似乎不起作用。

这是我的:

  <div class="container">

  <div class="row">
    <div class="col-md-6 mt-4 text-center">
      <table class="table table-striped">
        <tr>
          <th>ID</th>
          <th>TITLE</th>
          <th>NEWS</th>
          <th></th>
          <th></th>
        </tr>
        <% for(var i = 0; i < news.length; i++) { %>
          <tr>
            <td> <%= news[i].id_news %></td>
            <td> <%= news[i].title %></td>
            <td> <%= news[i].news %></td>
             <td>
               <input type="submit" name="" class="btn btn-link" value="Edit">
             </td>
             <td>
               <input type="submit" name="id_news" class="btn btn-link" value="Delete">
             </td>
            <!--<td><button type="button" name="del" class="close" aria-label="Close" >
                    <span aria-hidden="true">&times;</span>
                </button></td> -->

          </tr>
          <%  } %>
        </table>
      </div>
      <div class="col-md-6 mt-4 card">
        <form action="/news" method="post" class="card-body">
          <h3 class="card-title">Add News</h3>
          <div class="form-group">
            <input id="title" type="text" name="title" class="form-control" placeholder="Title">
          </div>
          <div class="form-group">
            <input type="text" name="news" class="form-control" placeholder="Content">
          </div>
          <input type="submit" class="btn btn-primary">

        </form>
      </div>
    </div>

  </div>

这是程序的另一部分,我尝试使用 DELETE FROM 表,但它不起作用。

const dbConnection = require('../../config/dbConnection');

module.exports = app => {
const connection = dbConnection();

app.get('/', (req, res) => {
connection.query('SELECT * FROM news', (err, result) => {
  console.log(result);
  res.render('news/news', {
    news: result
    });
  });
});

app.post('/news', (req, res) => {
const { title, news } = req.body;
connection.query('INSERT INTO news SET?', {
  title: title,
  news: news
}, (err, result) => {
  res.redirect('/');
  });
});

app.post('/news', (req, res) => {
const { id_news } =  req.body;
connection.query('DELETE FROM news WHERE id_news = ?', id_news , (err, 
result) => {
  res.redirect('/');
  });
});

最佳答案

看起来您的两条路由都在同一端点上定义:POST/news。 Express 只匹配第一个...它怎么知道差异?

您需要通过使用不同的 HTTP 方法来区分路由:

app.post('/news', (req, res) => { /* create */ })
app.delete('/news', (req, res) => { /* delete */ })

或不同路径:

app.post('/news', (req, res) => { /* create */ })
app.post('/delete-news', (req, res) => { /* delete */ })

并且 html 表单操作将需要相应地更新。

关于mysql - 如何删除选定的行mysql express.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515683/

相关文章:

javascript - 在 Nuxt.js 中禁用客户端水合或停止在 Nuxt.js 中公开原始数据

javascript - 在javascript中 promise 解决和拒绝

javascript - NodeJS 中的 ES6 : Arrow functions in object literal, 返回的 'this' 值是什么?

MySQL:添加上一个和当前作为字段 'academic-year' 的默认值

mysql - 使用 mysqlQuery 重复某些值时未获得预期结果

MySQL ORDER BY 带负数的数值问题

javascript - Nodejs 中间件在两个文件中

c# - 选择查询优化

node.js - 阻止函数被多次调用

javascript - 将异步函数传递给 Node.js Express.js 路由器