express.js未添加到mysql

6fe3ivhb  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(335)

我是expressjs的初学者,我希望能够将记录添加到“site”表中。但是,当我运行以下代码时,它会显示:

Error: ER_BAD_FIELD_ERROR: Unknown column 'BeastMode' in 'field list'.

“beastmode”是我在shortname字段中的一个条目。
一点背景:我不应该使用orm。我必须使用原始sql查询来添加到mysql数据库。我正在使用nodejs的'mysql'包来连接到数据库。

var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES "+
     "("+
      req.body.shortName+", "+
      req.body.addressLine1+", "+
      req.body.addressLine2+", "+
      req.body.city+", "+
      req.body.state+", " +
      req.body.zipcode+", " +
      req.body.phoneNumber+" );"    

     console.log(req.body);

     dbconnector.query(squery,function(err,rows,fields){
         if(!err){
             res.send("Record Added Successfully: "+req.body);
         }else{
             res.send("Error: "+ err);
         }
     });

    });

另外,这是我的dbconnect.js文件:

var mysql = require('mysql');

dbconnect = mysql.createConnection({
 host: "localhost",
 user: "root",
 password: "",
 database:"rsacs"
});

module.exports = dbconnect

这是我的html:

<!DOCTYPE html>
<html lang="en">
<head>
   <% include head %>
</head>
<body class="container">

<header>
   <% include header %>
</header>

<main>
   <div>
       <h1><%=title%></h1>
        <form method="post" action="/site/create" >
         <div class="form-group">   
            <label for="shortName">Shortname</label>
             <input type="text" class="form-control"  placeholder="Shortname"  name="shortName"><br>

             <label for="Address Line 1"> Address Line 1:</label>
             <input type="text" class="form-control"  placeholder="Address Line 1" name="addressLine1"><br>

             <label for="Address Line 2"> Address Line 2:</label>
             <input type="text"  class="form-control"  placeholder="Address Line 2" name="addressLine2"><br>

             <label for="City">City:</label>
             <input type="text"  class="form-control"  placeholder="City" name="city"><br>

             <label for="State">State:</label>
             <input type="text"  class="form-control"  placeholder="State" name="state"><br>

             <label for="Zipcode">Zipcode:</label>
             <input type="text"  class="form-control"  placeholder="Zipcode" name="zipcode"><br>

             <label for="PhoneNumber">Phone Number:</label>
             <input type="text"  class="form-control"  placeholder="PhoneNumber" name="phoneNumber"><br>

             <button type="submit" class="btn btn-primary">Submit</button>
         </div>                                                            
       </form>
   </div>
</main>

<footer>
   <% include footer %>
</footer>

</body>
</html>

这是我的站点表结构

w9apscun

w9apscun1#

为了响应@anshumanjaiswal的解决方案,您可能遇到了转义字符问题。
不过,我要提出的解决方案是不同的。mysql nodejs驱动程序支持准备好的查询。因此,对查询进行排序的最可靠的方法是:

var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES (?,?,?,?,?,?,?);
var objs =  [req.body.shortName,req.body.addressLine1,req.body.addressLine2,req.body.city,req.body.state,req.body.zipcode,req.body.phoneNumber]
sql = mysql.format(squery, objs);
// now you have a properly-escaped SQL query which you can execute as usual:
connection.query(squery, objs, function (error, results, fields) {if (error) throw error;});

如果这不能解决你的问题,请告诉我。

ukxgm1gy

ukxgm1gy2#

这些值是字符串,不能作为字符串传递。有两种可能的方法:
解决方案1。
将``添加到字符串值中,如:

var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES "+
     "('"+
      req.body.shortName+"', '"+
      req.body.addressLine1+"', '"+
      req.body.addressLine2+"', '"+
      req.body.city+"', '"+
      req.body.state+"', '" +
      req.body.zipcode+"', " +
      req.body.phoneNumber+" );"    
     ...

解决方案2。
从实体数据生成对象:

var data = {
    shortName: req.body.shortName,
    addressLine1: req.body.addressLine1,
    addressLine1: req.body.addressLine2,
    city: req.body.city,
    state: req.body.state,
    zipcode: req.body.zipcode,
    phoneNumber: req.body.phoneNumber
};
var squery = "INSERT INTO SITE SET ?";
dbconnector.query(squery, data, function(err,rows,fields){
     if(!err){
         console.log(rows);
         res.send("Record Added Successfully.");
     }else{
         res.send("Error: "+ err);
     }
 });

相关问题