使用node/js/MySQL工作台将变量传递给查询字符串

2023-12-01

问题:将变量传递到插入语句时,我在数据库中收到空插入。变量有名字、姓氏、电子邮件、密码。

我所知道的:我知道我正在获取用户输入表单数据。我正在安慰它们在插入语句之前存储的变量,该语句起作用并将表单数据打印到控制台。当表单在网页上提交时,它存储“firstName”、“LastName”、“email”、“passW”,这些都是变量。但是当我将它们传递到 MySQL Workbench 时,它们发布为空。我的结论是变量没有正确传递,但是当我尝试此页面和其他页面的建议时,它会抛出一个语法错误,表明 SQL 语法不正确。我尝试添加尽可能多的信息,如果您还需要我未提供的信息或需要我发布测试结果,LMK。

Code


var express = require("express");
var http = require("http");
var mysql = require("mysql");
var express = require("express");
var path = require("path");

var app = express();
var PORT = 3001;

// Sets up the Express app to handle data parsing
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public'))); // testing
app.use(express.json());
app.use("/assets", express.static("assets"))


// home page route
app.get("/", function(req, res) {
    res.sendFile(path.join(__dirname, "index.html"));
  });
// create account route
app.get("/create", function(req, res) {
  res.sendFile(path.join(__dirname, "create.html"))
});
// login route
app.get("/login", function(req, res) {
  res.sendFile(path.join(__dirname, "login.html"))
});


// DB Connection
var connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "sportsCorner",
  port: 3306,
});
// connection response 
connection.connect(function (err) {
  console.log("SQL connected as id " + connection.threadId)
});  


// Takes the data from our login form
app.post('/handler', function (req, res) {


  // user input from the forms
  var firstName = req.body.firstName;
  var lastName = req.body.lastName;
  var email = req.body.email;
  var passW = req.body.password;

  // testing our responses stored in the variables 
  console.log("F: " + firstName, "L: " + lastName, "E: " + email, "P: " + passW)

  connection.query("INSERT INTO loginInfo VALUES(firstName, lastName, email, passW)", function (err, res) {
    if (err) throw err;
    console.log("Inserted ...")
  });

  // Getting login info from DB
  connection.query('SELECT * from loginInfo', function (err, res) {
    if (err) throw err;
    console.log(res)
    console.log("Response ...")
  })
});


// Start Server
app.listen(PORT, function() {
  console.log("Server listening on: http://localhost:" + PORT);
});
   </script>
<HTML> (FORM)
 <form method="POST" action="/handler">
   <input type="text" name="firstName" placeholder="First Name">
   <input type="text" name="lastName" placeholder="Last Name">
   <input type="email" name="email" placeholder="Email">
   <input type="password" name="password" placeholder="Password">
   <input type="submit" />
 </form>
</HTML>

感谢您提前的帮助


使用它作为插入查询

var sql = `INSERT INTO loginInfo 
            VALUES
            (
                ?, ?, ?, ?
            )`;
connection.query(sql, [firstName, lastName, email, passW], function (err, res) {
    if (err) throw err;
    console.log("Inserted ...")
  });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用node/js/MySQL工作台将变量传递给查询字符串 的相关文章