[英]Adding data to mysql database with node.js
I'm new to programming and trying to create a basic app with node.js. 我是编程和尝试使用node.js创建基本应用程序的新手。
I'm using node.js on visual studio with html instead of jade. 我在带有html而不是jade的visual studio上使用node.js。 I have a simple form with name, surname and gender.
我有一个简单的表格,里面有名字,姓氏和性别。 I have also managed to set up a database with mysql workbench.
我也设法用mysql workbench建立了一个数据库。
I have the following query which adds the name, surname and gender successfully to the mysql database when I run the code. 我有以下查询,当我运行代码时,该查询将姓名,姓氏和性别成功添加到mysql数据库中。
var user = { "name": "Name", "surname": "Surname", "gender" : "M" };
connection.query('INSERT INTO studentinfo SET ?', user, function (err, res) {
if (err) throw err;
});
But I am stuck when the data has to be collected from the form when i click the submit button. 但是,当我单击提交按钮时,必须从表单中收集数据时,我陷入了困境。
I guess its something to do with Ajax, I found some solutions with php, but I'm struggling with finding solutions with node.js and how and where to implement the code. 我想这与Ajax有关,我找到了php的一些解决方案,但是我在努力寻找与node.js的解决方案以及实现代码的方式和地点。
Here is the app.js file 这是app.js文件
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'my_password',
database: 'userlist',
});
connection.connect();
var user = { "name": "RandomName", "surname": "RandomSurnameName", "gender" : "M" };
connection.query('INSERT INTO studentinfo SET ?', user, function (err, res) {
if (err) throw err;
});
//connection.query('SELECT * FROM studentinfo', function (err, rows) {
// if (err) throw err;
// console.log('Data received from Db:\n');
// console.log(rows);
//});
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/about', routes.about);
app.get('/contact', routes.contact);
app.get('/newStudent', routes.newStudent);
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
var createStudent = {
name: String,
surname: String,
dob: Date,
gender: String,
}
connection.query('insert into userlist set ?', createStudent, function (err, result) {
});
Ok, let's give it a shot. 好吧,让我们试一下。
On the HTML page, you need to submit the student details. 在HTML页面上,您需要提交学生详细信息。 A very very basic form looks like this:
一个非常非常基本的形式如下所示:
<form action="/student" method="post">
First name:<br>
<input type="text" name="name"<br>
Last name:<br>
<input type="text" name="surname"><br>
Date of birth:<br>
<input type="text" name="dob"<br>
Gender:<br>
<input type="text" name="gender"><br><br>
<input type="submit" value="Submit">
</form>
This is obviously oversimplified, but it will do the trick. 这显然被简化了,但是可以解决问题。 So you have some boxes and a button that will
POST
this data to your server /student
route. 所以,你有一些箱子和一个按钮,将
POST
这个数据到你的服务器/student
航线。
Now we need to handle that route on the express server. 现在,我们需要在快递服务器上处理该路由。 You already have
GET
routes, so add this: 您已经有
GET
路线,因此添加以下内容:
app.post('/', function (req, res) {
// this is where you handle the POST request.
var createStudent = {
name: req.body.name,
surname: req.body.surname,
dob: req.body.dob,
gender: req.body.gender
}
// now the createStudent is an object you can use in your database insert logic.
connection.query('INSERT INTO studentinfo SET ?', createStudent, function (err, resp) {
if (err) throw err;
// if there are no errors send an OK message.
res.send('Saved succesfully');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.