Express.js没有添加到mysql中

时间:2018-04-09 02:27:33

标签: mysql node.js express

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

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

“BeastMode”是我对shortName字段的一个条目。

一点背景:我不应该使用ORM。我必须使用原始的sql查询添加到MYSQL数据库。我使用'mysql'包为Nodejs连接到数据库。

 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>

Here is my Site table structure

2 个答案:

答案 0 :(得分:1)

为了回应@ AnshumanJaiswal的解决方案,您可能遇到了转义字符问题。

但我提议的解决方案是不同的。 mysql nodejs驱动程序supports prepared queries。因此,对查询进行排序的最有效方法是:

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;});

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

答案 1 :(得分:0)

值是字符串,您没有将它们作为字符串传递。 有两种可能的方式:

解决方案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);
     }
 });