javascript - 将数据从 html 表单发布到 SQL 不会发布

标签 javascript node.js azure

我有一个 Node.js Web 应用程序、一个 SQL 服务器,并且我在 Visual Studio 代码中本地工作,然后同步到 Azure。

我的主页上有一个简单的表单,当我单击“发布”时,应该将数据发送到我的 SQL 数据库中的表中。然而什么也没有发生。我被重定向到用户页面,并且没有收到任何错误,但我在控制台日志中也没有收到任何内容,也没有收到任何发布到表中的内容。

这是我所拥有的:

app.js

var express = require('express');
var app = express();
var mysql = require("mysql");
var bodyParser = require('body-parser');

var config = {
      host: '',
      user: '',
      password: '',
      database: '',
      port: 3306,
      ssl: true
    };

var connection = new mysql.createConnection(config);

app.use('/node_modules',  express.static(__dirname + '/node_modules'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/Template/views/home.html',function(req,res){
  res.sendFile('home.html',{'root': __dirname + '/views'});
})

app.get('/Template/views/about.html',function(req,res){
  res.sendFile('about.html',{'root': __dirname + '/views'});
})

app.get('/Template/views/user.html',function(req,res){
  res.sendFile('user.html',{'root':__dirname + '/views'})
})

app.post('/Template/views/user.html', function(req, res){   
  var fName = req.body.first_name;
  connection.query("INSERT INTO `Persons` (name) SET ?", fName.toString(), function(err, result) { function(err, result) {
    if(err) throw err;
      console.log("1 record inserted");
    });
    res.send('user.html',{'root':__dirname + '/views'})
  });

app.listen(process.env.PORT);

home.html

<!DOCTYPE html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="description" content="">
      <meta name="author" content="">
      <title>Template</title>
      <link href="../node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    </head>
    <body>
      <form action="/Template/views/user.html" name="formA">
        First Name: <input type="text" name="first_name"> <br>
        Last Name: <input type="text" name="last_name"> <br>
        <input type="submit" value="submit">        
      </form>
    </body>
</html>

有人能看到我在这里做错了什么吗?

最佳答案

定义连接后尝试连接:

connection.connect();

然后执行查询并最终关闭连接:

connection.end();

关于javascript - 将数据从 html 表单发布到 SQL 不会发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55947907/

相关文章:

javascript - Angular4 : Router not working. 路由错误:错误:序列中没有元素

javascript - 连接 Action 以存储在组件外部?

node.js - 如何在 Knex.js 中动态创建 where 条件?

javascript - 如何将后端(Express JS)提供的静态文件显示到React页面中?

node.js - 如何将 QuickReport .QRP 文件转换为 XML、HTML 或文本?

azure - 是否有任何规定可以在 Azure 门户本身中更改 Azure AD 用户的密码过期时间

azure - APIM 策略 'Validate-header' 对于 'Permissions-Policy' header 失败,即使其有效值也是如此

node.js - 尝试获取 Azure Blob 存储中非公开的文件时,Azure 函数收到 AuthorizationFailure

javascript - 如何在 bootstrap 4 中居中对齐 div

javascript - AWS 授权方返回 500,消息 : null,,响应中出现 AuthorizerConfigurationException 错误