gpt4 book ai didi

mysql - 如何使用 Node 和 express 向 MySQL 表中插入数据?

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:30 25 4
gpt4 key购买 nike

我将 Node 与 express 和 MySQL 一起用于数据库。我创建了一个 ejs 文件,其中有一个表单 (method=POST)

在我的服务器文件中,我可以检索该表单发送的数据,我什至可以对其进行控制台记录并获得所需的输出。由于表单中有多个条目,我使用不同的变量将这些条目存储在我的服务器文件中。

在查询中,当我将这些变量传递到数据库中时,数据库只插入 Null,而不是变量中存储的实际数据。

表单代码如下:

    <form action="/newuser" style="border:1px solid #ccc" method="post">
<div class="container">
<h1>Sign Up</h1>
<p>Please fill in this form to create an account.</p>
<hr>

<label for="Name"><b>Name</b></label>
<input type="text" placeholder="Enter Name" name="name" required>

<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>


<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>

<hr>

<label for="address"><b>Address</b></label>
<input type="text" placeholder="House no and Street" name="address" required>


<label for="city"><b>City</b></label>
<input type="text" placeholder="Enter City" name="city" required>

<label for="pin"><b>Postal Code</b></label>
<input type="number" placeholder="Enter pin" name="pin" required>

<label for="country"><b>Country</b></label>
<input type="text" placeholder="Enter Country" name="country" required>

<label for="mobile"><b>Mobile Number</b></label>
<input type="number" placeholder="Enter Mobile Number" name="mobile" required>




<div class="clearfix">

<a href="/"><button type="button" class="cancelbtn">Cancel</button></a>
<button type="submit" class="signupbtn">Sign Up</button>
</div>
</div>
</form>

服务器文件中的代码如下:

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

app.use( express.static( "public" ) );
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));


var connection = mysql.createConnection({
host: "localhost",
user:"root",
password:"",
database:"bakery"
});


//some more code to get different routes

connection.connect(function(error){
if(error){
console.log("Error while connecting to database")
}
else{

// console.log("connected");
connection.query("SELECT * FROM products", function (err, result, fields) {
if (err) throw err;
for (var i = 0; i < result.length; i++) {
var row = result[i];
console.log(row.ProductName, "costs", row.Price, "and its from category", row.Category );
}
// console.log(result);
})
}});


app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");

app.post("/newuser", function(req, res) {
// get data from forms and add to the table called user..

var name = req.body.name;
var email = req.body.email;
var password = req.body.psw;
var city = req.body.city;
var address = req.body.address;
var country= req.body.country
var pin = req.body.pin;
var mobile = req.body.mobile;

console.log(name, mobile, pin);

connection.query("INSERT INTO user (Name, Email, Address, City, Country, password) VALUES", (name , email, address, city, country , password), function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
res.redirect("/");

使用此代码,无论我在表单中键入什么,它都只会将 Null 插入数据库。

最佳答案

像这样尝试:

var sql = `INSERT INTO user 
(
Name, Email, Address, City, Country, password
)
VALUES
(
?, ?, ?, ?, ?, ?
)`;
connection.query(sql, [name , email, address, city, country , password], function (err, data) {
if (err) {
// some error occured
} else {
// successfully inserted into db
}
});

关于mysql - 如何使用 Node 和 express 向 MySQL 表中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49821318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com