added update post

This commit is contained in:
Sergey Yarkov 2019-10-08 19:15:33 +03:00
parent 184c03aeaf
commit d0f3f49db0
4 changed files with 25 additions and 14 deletions

View File

@ -73,25 +73,23 @@ app.get('/delete/:id', (req, res) => {
// запрос на редактирование
app.post('/update', (req, res) => {
const id = req.body.id,
title = req.body.title,
date = req.body.date,
description = req.body.description;
const data = {
id: req.body.id,
title: req.body.title,
date: req.body.date,
description: req.body.description
};
for (let k in data) {
console.log(k + ' : ' + data[k]);
}
res.send(`<p>ID: ${id}<br>Название: ${title}<br>Дата: ${date}<br>Описание: ${description}</p>`);
conn.query(
`UPDATE posts SET title='${data.title}', date='${data.date}', description='${data.description}' WHERE id=${data.id}`,
function(err, result) {
if (!err) {
res.send('Пост был успешно отредактирован');
} else {
res.status(404).send('<h1>404 Not found</h1>');
}
}
);
});
// UPDATE `blog`.`posts` SET `description` = 'gfhj' WHERE (`id` = '10');
// 404 redirect error
app.use((req, res) => {

View File

@ -57,4 +57,15 @@ input[type="date"] {
}
.modal {
min-width: 500px;
}
.post-block_add {
border: 6px dashed;
text-align: center;
padding: 70px 0px 70px 0px;
cursor: pointer;
transition: all .2s;
margin-top: 3rem;
}
.post-block_add:hover {
opacity: .9;
}

View File

@ -16,10 +16,10 @@ function updatePost() {
btnSend.addEventListener('click', (e) => {
let idInputValue = idInput.value;
if (!isNaN(idInputValue) == false) {
alert('Поле ID должен быть числом!');
e.preventDefault();
e.preventDefault();
} else {
idInputValue = idInputValue.trim();
}

View File

@ -4,4 +4,6 @@ div(class="container")
h1 Все записи:
include assets/update-form.pug
include assets/post-card.pug
div(class="post-block_add")
h3 Добавить запись
include assets/scripts.pug