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

View File

@ -57,4 +57,15 @@ input[type="date"] {
} }
.modal { .modal {
min-width: 500px; 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) => { btnSend.addEventListener('click', (e) => {
let idInputValue = idInput.value; let idInputValue = idInput.value;
if (!isNaN(idInputValue) == false) { if (!isNaN(idInputValue) == false) {
alert('Поле ID должен быть числом!'); alert('Поле ID должен быть числом!');
e.preventDefault(); e.preventDefault();
} else { } else {
idInputValue = idInputValue.trim(); idInputValue = idInputValue.trim();
} }

View File

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