最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

[我想使用node.js在输入框,表或div中显示.html文件中存储在mysql数据库中的数据

IT培训 admin 2浏览 0评论

[我想使用node.js在输入框,表或div中显示.html文件中存储在mysql数据库中的数据

我能够使用node.js连接到数据库,并使用车把从数据库中检索数据,但不能使用.html扩展名发送数据。只要保存在.html扩展名中的任何方法都对我有用。这是我的把手文件:-

<table class="table " id="mytable">
  <thead>
    <tr>
      <th>Sr.Number</th>
      <th>Quote</th>
      <th>Author</th>
    </tr>
  </thead>
  <tbody>
     {{#each results}}
    <tr>
      <td>{{ id }}</td>
      <td>{{ quote }}</td>
      <td>{{ author}}</td>

    </tr>
    {{/each}}
  </tbody>
</table>

这是我的.js文件,它连接数据库并将视图引擎设置为hbs,并且还传递mysql查询以选择数据库的所有项目:-

    const path = require('path');
    const express = require('express');
    const hbs = require('hbs');
    const bodyParser = require('body-parser');
    const mysql = require('mysql');
    const app = express();

    const conn = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: 'password',
      database: 'theblog'
    });
    conn.connect((err) =>{
      if(err) throw err;
      console.log('Mysql Connected...');
    });

    //set views file
    app.set('views',path.join(__dirname,'views'));
    //set view engine
    app.set('view engine', 'hbs');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    //set public folder as static folder for static file
    app.use('/public',express.static(__dirname + '/public')); 

    //route for homepage
    app.get('/',(req, res) => {
      let sql = "SELECT * FROM quote_gall";
      let query = conn.query(sql, (err, results) => {
        if(err) throw err;
        res.render('quote',{
          results: results

             });
      });
    });

      app.listen(2121);
      console.log("Running at Port 2121");

我希望得到帮助。预先感谢。

回答如下:

编写sql!您可以使用“插入”命令。

let sql = "INSERT INTO *TABLE_NAME* (calumn1, calumn2, calumn3) VALUES (value1, value2, value3)";
conn.query(sql, (err, results) => {
    //~
}

如果要添加数据,可以使用该查询。

[我想使用node.js在输入框,表或div中显示.html文件中存储在mysql数据库中的数据

我能够使用node.js连接到数据库,并使用车把从数据库中检索数据,但不能使用.html扩展名发送数据。只要保存在.html扩展名中的任何方法都对我有用。这是我的把手文件:-

<table class="table " id="mytable">
  <thead>
    <tr>
      <th>Sr.Number</th>
      <th>Quote</th>
      <th>Author</th>
    </tr>
  </thead>
  <tbody>
     {{#each results}}
    <tr>
      <td>{{ id }}</td>
      <td>{{ quote }}</td>
      <td>{{ author}}</td>

    </tr>
    {{/each}}
  </tbody>
</table>

这是我的.js文件,它连接数据库并将视图引擎设置为hbs,并且还传递mysql查询以选择数据库的所有项目:-

    const path = require('path');
    const express = require('express');
    const hbs = require('hbs');
    const bodyParser = require('body-parser');
    const mysql = require('mysql');
    const app = express();

    const conn = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: 'password',
      database: 'theblog'
    });
    conn.connect((err) =>{
      if(err) throw err;
      console.log('Mysql Connected...');
    });

    //set views file
    app.set('views',path.join(__dirname,'views'));
    //set view engine
    app.set('view engine', 'hbs');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    //set public folder as static folder for static file
    app.use('/public',express.static(__dirname + '/public')); 

    //route for homepage
    app.get('/',(req, res) => {
      let sql = "SELECT * FROM quote_gall";
      let query = conn.query(sql, (err, results) => {
        if(err) throw err;
        res.render('quote',{
          results: results

             });
      });
    });

      app.listen(2121);
      console.log("Running at Port 2121");

我希望得到帮助。预先感谢。

回答如下:

编写sql!您可以使用“插入”命令。

let sql = "INSERT INTO *TABLE_NAME* (calumn1, calumn2, calumn3) VALUES (value1, value2, value3)";
conn.query(sql, (err, results) => {
    //~
}

如果要添加数据,可以使用该查询。

发布评论

评论列表 (0)

  1. 暂无评论