简体   繁体   中英

retrieve data from database into html using node js by search button

I want to display data into html table rows from columns like (survey_n,NAME,PLACE) from database based on search button. I want to search data based on survey_n.

I have errors at sql selection part:


var express = require('express');
var connect = require("connect")


app = express().use(express.static(__dirname + '/db')),
http = require('http').Server(app);

const { Pool, Client } = require('pg')
const connectionString = 'postgresql://postgres:postgr@localhost:5432/apb_cad'
const pool = new Pool({
  connectionString: connectionString,
})
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/search', function (req, res, next) {
  if(req.method === "POST"){
       pool.query('SELECT survey_n_1,lulc,gm FROM table',function(err,rows,fields) {
           if(err){
               console.log(err);
               res.status(400).send(err);
           }
res.render('home.html',{data: rows.rows});

       });
     }
    });


app.listen(4000, function () {
    console.log('Server is running.. on Port 4000');
});

<form class="example" action="/search" method="POST">
  <input type="text" placeholder="Search..">
  <button type="submit"  value="Submit"><i class="fa fa-search"></i></button>
</form>

<table>
    <th>Surevy N</th>
    <th>L</th>
    <th>G</th>

  <tr>
    <% for(var i=0; i< data.length; i++) { %>
                <td><%= data[i].survey_n%></td>
                <td><%= data[i].lu%></td>
                <td><%= data[i].g%></td>  
            </tr>
            <% } %>
</table>


table is a reserved keyword in PostgreSQL , you can't use it like that.

Replace this line:

pool.query('SELECT survey_n_1,lulc,gm FROM table',function(err,rows,fields) {

by:

pool.query('SELECT survey_n_1,lulc,gm FROM `table`',function(err,rows,fields) {

Better yet, replace the name of your table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM