<?php
$pdo = new PDO('mysql:host=localhost;dbname=personal_website;charset=utf8', 'root', 'root', [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
<?php
require_once "db.php";
$stmt = $pdo->query("select * from works");
$works = $stmt->fetchAll();
?>
<!DOCTYPE html>
...
...
<div id="lightgallery" class="gallery">
<a class="img-wrapper" href="assets/img/placeholder.jpg">
<img src="assets/img/placeholder.jpg" />
</a>
</div>
...
<div id="lightgallery" class="gallery">
<?php foreach($works as $work): ?>
<a class="img-wrapper"
data-sub-html="<?= $work['name'] ?>"
href="<?= $work['file_path'] ?>">
<img src="<?= $work['file_path'] ?>" />
</a>
<?php endforeach; ?>
</div>
<form action="feedback.php" method="POST">
...
</form>
<form action="feedback.php" method="POST">
<input name="name" required type="text" placeholder="Как к вам обращаться">
<input name="email" required type="email" placeholder="Ваш email">
<textarea name="text" required rows="4" placeholder="Сообщение"></textarea>
<input class="btn btn-bg" type="submit" value="Отправить">
</form>
<?php
require_once "db.php";
if( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['text']) )
{
...
}
if( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['text']) )
{
$stmt = $pdo->prepare("insert into messages(name, email, text) values(?,?,?)");
}
if( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['text']) )
{
$stmt = $pdo->prepare("insert into messages(name, email, text) values(?,?,?)");
$stmt->execute([
$_POST['name'],
$_POST['email'],
$_POST['text']
]);
}
...
header("Location: index.php");
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Админка</title>
<link rel="stylesheet" href="../assets/css/main.css">
</head>
<body>
</body>
</html>
<?php
require_once "../db.php";
$stmt = $pdo->query("select * from messages");
$messages = $stmt->fetchAll();
?>
<!DOCTYPE html>
...
<body>
<div class="container">
<h2>Сообщения</h2>
<table>
<tr>
<th>#</th>
<th>Имя</th>
<th>Email</th>
<th>Текст</th>
<th>Дата и время</th>
</tr>
<tr>
<td>1</td>
<td>Имя</td>
<td>Почта</td>
<td>Сообщение</td>
<td>Дата и время</td>
</tr>
</table>
</div>
</body>
<body>
<div class="container">
<h2>Сообщения</h2>
<table>
<tr>
<th>#</th>
<th>Имя</th>
<th>Email</th>
<th>Текст</th>
<th>Дата и время</th>
</tr>
<?php foreach ($messages as $key => $message) : ?>
<tr>
<td><?= $key + 1 ?></td>
<td><?= htmlspecialchars($message['name']) ?></td>
<td><?= htmlspecialchars($message['email']) ?></td>
<td><?= htmlspecialchars($message['text']) ?></td>
<td><?= $message['created_at'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</body>
<table border="1">
...
</table>
...
<link rel="stylesheet" href="../assets/css/main.css">
<style>
table {
border-collapse: collapse;
width: 100%;
}
td,
th {
padding: 10px;
}
</style>
...
...
$messages = $stmt->fetchAll();
$stmt = $pdo->query("select * from works");
$works = $stmt->fetchAll();
?>
...
<h2>Портфолио</h2>
<div id="lightgallery" class="gallery">
<?php foreach ($works as $work) : ?>
<a class="img-wrapper"
data-sub-html="<?= $work['name'] ?>"
href="http://website/<?= $work['file_path'] ?>">
<img src="http://website/<?= $work['file_path'] ?>" />
</a>
<?php endforeach; ?>
</div>
<br><br><br><br>
<h2>Сообщения</h2>
...
<style>
...
.admin-img-wrapper {
margin-bottom: 30px;
}
.admin-img-wrapper .img-wrapper {
margin-bottom: 0;
}
</style>
...
<div id="lightgallery" class="gallery">
<?php foreach ($works as $work) : ?>
<div class="admin-img-wrapper">
<a class="img-wrapper"
data-sub-html="<?= $work['name'] ?>"
href="http://website/<?= $work['file_path'] ?>">
<img src="http://website/<?= $work['file_path'] ?>" />
</a>
<a href="remove.php?id=<?= $work['id'] ?>">Удалить</a>
</div>
<?php endforeach; ?>
</div>
<?php
require_once "../db.php";
if(isset($_GET['id']))
{
$stmt = $pdo->prepare('select * from works where id = ?');
$stmt->execute([$_GET['id']]);
$work = $stmt->fetch();
}
…
$work = $stmt->fetch();
if($work) {
$stmt = $pdo->prepare('delete from works where id = ?');
$stmt->execute([$_GET['id']]);
unlink( dirname(dirname(__FILE__)).'/'.$work['file_path'] );
}
}
…
header('Location: index.php');
...
<h2>Портфолио</h2>
<a href="add.php">Добавить</a>
...
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Добавление работы в портфолио</title>
</head>
<body>
<form action="add.php" method="post" enctype="multipart/form-data">
<input required name="name" type="text" placeholder="Название">
<input required name="file" type="file">
<input type="submit" value="Создать">
</form>
</body>
</html>
<?php
require_once "../db.php";
if ( !empty($_POST['name']) ) {
...
}
?>
<!DOCTYPE html>
...
...
{
$apppath = dirname(dirname(__FILE__));
$filepath = 'uploads/' . time() . basename($_FILES['file']['name']);
$uploadfile = $apppath . '/' . $filepath;
}
...
...
$uploadfile = $apppath . '/' . $filepath;
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
...
...
$stmt = $pdo->prepare("insert into works(name, file_path) values(?,?)");
$stmt->execute([
$_POST['name'],
$filepath
]);
header("Location: index.php");
}
...
| Стартуем! |
| Дальше |
| Проверить |
| Узнать результат |
| Дальше |
| Проверить |
| Узнать результат |
| Дальше |
| Проверить |
| Узнать результат |
| Дальше |
| Проверить |
| Узнать результат |
| Дальше |
| Проверить |
| Узнать результат |
| Пройти еще раз |
| Пройти еще раз |
| Пройти еще раз |