我正在尝试使用HTML表单中的节点和Express框架将数据插入MySQL。表单的代码是:
<html>
<head>
<title>Personal Information</title>
</head>
<body>
<div id="info">
<h1>Personal Information</h1>
<form action="/myaction" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name" />
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email address" />
<br><br>
<label for="city">City:</label>
<input type="text" id="city" name="city" placeholder="Enter your city" />
<br><br>
<label for="pincode">Pincode:</label>
<input type="text" id="pincode" name="pincode" placeholder="Enter your pincode" />
<br><br>
<input type="submit" value="Send message" />
</form>
</div>
</body>
</html>
和.js文件是:
var express = require('express');
var app = express();
var ejs = require('ejs');
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
var HOST = 'localhost';
var PORT = 3000
var MYSQL_USER = 'root';
var MYSQL_PASS = 'jelly123#';
var DATABASE = 'form';
var TABLE = 'info';
var mysql = mysql.createConnection({
host: HOST,
port: PORT,
user: MYSQL_USER,
password: MYSQL_PASS,
});
app.get('/home',function(req,res,next){
res.sendfile('views/forms.html');
});
app.post('/myaction', function(req, res) {
console.log('req.body');
console.log(req.body);
res.write('You sent the name "' + req.body.name+'".\n');
res.write('You sent the Email "' + req.body.email+'".\n');
res.write('You sent the City "' + req.body.city+'".\n');
res.write('You sent the Pincode "' + req.body.pincode+'".\n');
res.end()
mysql.query("Insert into "+TABLE+" (name,email,city,pincode) VALUES ('"+req.body.name+"','"+req.body.email+"','"+req.body.city+"','"+req.body.pincode+"')",function(err, result)
{
if (err)
throw err;
});
});
app.listen(3000);
console.log('Example app listening at port:3000');
我可以输入表单数据并将其显示在页面http://localhost:3000/myaction中,但无法将数据插入数据库请提及我在哪里做错了。
提前致谢。
答案 0 :(得分:2)
var express = require('express');
var app = express();
var ejs = require('ejs');
var pg = require('pg');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
var conString = process.env.DATABASE_URL || "postgres://postgres:Emdsystems@localhost:5432/student";
var client = new pg.Client(conString);
client.connect();
app.get('/',function(req,res,next){
res.sendfile('views/forms.html');
});
app.post('/myaction', function(req, res) {
console.log('req.body');
console.log(req.body);
res.write('You sent the name "' + req.body.name+'".\n');
res.write('You sent the Email "' + req.body.email+'".\n');
res.write('You sent the City "' + req.body.city+'".\n');
res.write('You sent the Pincode "' + req.body.pincode+'".\n');
res.end()
client.query("Insert into record (name,email,city,pincode) VALUES ('"+req.body.name+"','"+req.body.email+"','"+req.body.city+"','"+req.body.pincode+"')",function(err, result)
{
if (err)
throw err;
});
});
app.listen(3000);
console.log('Example app listening at port:3000');
答案 1 :(得分:1)
试试这个
mysql.query('select id, name, price from ' + TABLE + ' where price < 100',
function(err, result, fields) {
if (err) throw err;
else {
console.log('Gadgets which costs less than $100');
console.log('----------------------------------');
for (var i in result) {
var gadget = result[i];
console.log(gadget.name +': '+ gadget.price);
}
}
});
答案 2 :(得分:1)
这是我的js文件 -
var express = require("express");
var app = express();
var path = require("path");
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb"
});
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
});
app.post('/submit',function(req,res){
var name=req.body.name;
var email=req.body.email;
var username=req.body.username;
res.write('You sent the name "' + req.body.name+'".\n');
res.write('You sent the email "' + req.body.email+'".\n');
res.write('You sent the username "' + req.body.username+'".\n');
con.connect(function(err) {
if (err) throw err;
var sql = "INSERT INTO form (name, email,description) VALUES ('"+name+"', '"+email+"','"+username+"')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
res.end();
});
});
})
app.listen(3000);
console.log("Running at Port 3000");
这是我的html文件 -
<html>
<head>
<title> test </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<form action="/submit" method="POST">
<fieldset>
<label for="name">Name: </label>
<input type="text" id="name" name="name" autofocus />
<br/>
<label for="email">Email: </label>
<input type="email" id="email" name="email" />
<br/>
<label for="username">User name: </label>
<input type="textbox" id="username" name="username" />
<br/>
<input type="submit" value="create profile" />
</fieldset>
</form>
</body>
</html>
答案 3 :(得分:0)
你可以试试这个
我在节点中使用快速js这样做 先决条件:安装节点js,mysql,express,express-generator
将此内容写入index.jade文件
<html>
<head>
<title> test </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<form action="/submitform/" method="POST">
<fieldset>
<label for="name">Name: </label>
<input type="text" id="name" name="name" autofocus />
<br/>
<label for="email">Email: </label>
<input type="email" id="email" name="email" />
<br/>
<label for="description">Description: </label>
<textarea id="description" name="description"></textarea>
<br/>
<input type="submit" value="create profile" />
</fieldset>
</form>
</body>
</html>
然后,在index.js文件中写这个
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "testn"
});
router.post('/submitform', function(req, res, next) {
console.log(req.body.name);
console.log(req.body.email);
console.log(req.body.description);
con.connect(function(err) {
if (err) throw err;
console.log("connected");
var sql = "INSERT INTO `form`(`name`,`email`, `description`) VALUES ('"+req.body.name+"','"+req.body.email+"','"+req.body.description+"')";
con.query(sql, function(err, result) {
if(err) throw err;
console.log("table created");
});
});
res.render('index', { title: 'Express' });
});
module.exports = router;
此代码将从表单中将值插入到节点js
中的MySql中希望这会对你有所帮助