adduser.js
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var express = require('express');
var fs = require('fs');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'soap',
password : '111111',
port : 3306,
database : 'ytmt'
});
router.get('/', function(req, res) {
var username = [];
connection.query("SELECT username FROM user WHERE id = " + req.user.user_id,[username], function(err, result) {
if(!err) {
if(result[0].username == null) {
fs.readFile('./views/adduser.ejs', function(err, data) {
if(err) {console.log(err);}
else {
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end(data);
}
});
}else {
res.redirect('/mytoons');
}
}
else {
console.log('DB : ERROR OCCURS');
}
});
});
router.post('/addtodb', (req, res) => {
var user_name = req.body.username;
var sqlquery = "UPDATE user SET username = '" + user_name +"' WHERE id = " + req.user.user_id;
connection.query(sqlquery,user_name,function(err) {
if(err) {
res.send("DATA BASE : ERROR OCCURS");
}
else {
// res.send("SUCCESS!");
res.redirect('/mytoons');
}
})
})
module.exports = router;