【问题标题】:How to delete a selected row mysql express.js如何删除选定的行mysql express.js
【发布时间】:2018-09-06 00:39:51
【问题描述】:

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

这就是我所拥有的:

  <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('/');
  });
});

}

【问题讨论】:

    标签: mysql node.js express ejs


    【解决方案1】:

    看起来你的两个路由都定义在同一个端点上: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 表单操作也需要相应更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2018-06-04
      • 1970-01-01
      • 2012-07-30
      • 2013-03-21
      • 2017-09-30
      • 2014-12-17
      相关资源
      最近更新 更多