如何通过Node.js执行多个SQL查询

时间:2019-06-20 20:03:46

标签: javascript node.js

我正在尝试通过我的节点js执行sql查询,我已经成功地使用数据库验证了我的登录页面,并且成功验证之后我能够获得主页,在这里我有另外一个要求...再次我如果客户单击按钮搜索但未执行第二次查询,则需要从主页与数据库进行通信以显示所有产品,请帮帮我

预先感谢。...

登录验证:

app.post('/Login', function(req,res){     
    console.log(req.body);
     const check = "select count(*) as cnt from users_info where Name ='"+req.body.userid+"' && Password = '"+req.body.pswrd+"' ";
    mysqlConnection.query(check,function(err,result){
        console.log(result);
        var noOfRecords = result[0].cnt;
        console.log(noOfRecords);
        if (noOfRecords == 1)
        {
            res.render('Login', {username:req.body.userid});
        }
        else
           console.log('User name or Password is invalid');
            //  res.render('Login.ejs', {error: 'User name or Password is invalid'});
    }); 
    mysqlConnection.end();  
    }); 

产品列表用途:

app.post("/Login/search", (req,res) =>{
    console.log(req.body.search) 
    const check = "select * from Items_List where product NOT LIKE '${req.body.search}%' ";//this query is not executed 

    mysqlConnection.query(check,function(err,result){
        const arr = result ;
        console.log(app)
        console.log(arr.length)
        let countRecords = arr.length;
        if (countRecords>0){
            const names = arr.map(({ Name }) => Name).join('\n');
            console.log(names.length)
            console.log(names)

            res.render('Login-sucess', {username:names});

            // res.send(names);
        } else {
            console.log('Request failed...')
        }
     }); 
    mysqlConnection.end();  
});

0 个答案:

没有答案