-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdateInformation.js
More file actions
49 lines (43 loc) · 2.04 KB
/
updateInformation.js
File metadata and controls
49 lines (43 loc) · 2.04 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
const express = require('express');
const router = express.Router();
const conn = require('./connectDB'); // Đảm bảo bạn đã kết nối với cơ sở dữ liệu
const countryList = require('./countryList'); // Đường dẫn đến file countryList.js
router.post('/update-information', (req, res) => {
const { userID, bio, address, country, phone } = req.body;
const sql = "UPDATE User SET bio = ?, address = ?, country = ?, phone = ? WHERE userID = ?";
conn.query(sql, [bio, address, country, phone, userID], (err) => {
if (err) {
console.log(err)
console.error(err);
return res.status(500).send("Error updating information.");
}
// Thông báo thành công
error_message = ''
success_message = "Cập nhật thông tin thành công!";
// Lấy thông tin mới nhất của người dùng từ cơ sở dữ liệu
const sqlSelectUser = `SELECT * FROM user WHERE userID = ?`;
conn.query(sqlSelectUser, [userID], (err, result) => {
if (err) {
console.error('Lỗi:', err.message);
return res.status(500).send('Có lỗi xảy ra.');
}
// Gán lại tất cả thông tin của userLogin vào session
req.session.userLogin = {
userID: result[0].userID,
userName: result[0].userName,
email: result[0].email,
image: result[0].image,
loginpassword: result[0].loginpassword,
address: result[0].address,
bio: result[0].bio,
country: result[0].country,
phone: result[0].phone
};
// Truyền thông tin người dùng vào `res.render`
const success_message = 'Update Information Successfully';
const website = 'Information.ejs';
res.render('Information', { userLogin: req.session.userLogin, website, success_message, countryList });
});
});
});
module.exports = router;