Answer the question
In order to leave comments, you need to log in
How to make a personal task board?
There is a to-do list with registration. It is necessary to make sure that each user has their own personal task board. I was able to make sure that tasks from the board are written to the database, but I don’t know how to display them on the page.
PS: I know that it's better to work through PDO, but it's easier at the moment...
Connecting to the database
<?php
$connect = mysqli_connect('localhost', 'root', '', 'test');
if (!$connect) {
die('Error connect to DataBase');
}
<?php
session_start();
require_once 'connect.php';
$full_name = $_POST['full_name'];
$login = $_POST['login'];
$email = $_POST['email'];
$password = $_POST['password'];
$password_confirm = $_POST['password_confirm'];
$check_login = mysqli_query($connect, "SELECT * FROM `users` WHERE `login` = '$login'");
if (mysqli_num_rows($check_login) > 0) {
$response = [
"status" => false,
"type" => 1,
"message" => "Такой логин уже существует",
"fields" => ['login']
];
echo json_encode($response);
die();
}
$error_fields = [];
if ($login === '') {
$error_fields[] = 'login';
}
if ($password === '') {
$error_fields[] = 'password';
}
if ($full_name === '') {
$error_fields[] = 'full_name';
}
if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_fields[] = 'email';
}
if ($password_confirm === '') {
$error_fields[] = 'password_confirm';
}
if (!$_FILES['avatar']) {
$error_fields[] = 'avatar';
}
if (!empty($error_fields)) {
$response = [
"status" => false,
"type" => 1,
"message" => "Проверьте правильность полей",
"fields" => $error_fields
];
echo json_encode($response);
die();
}
if ($password === $password_confirm) {
$path = 'uploads/' . time() . $_FILES['avatar']['name'];
if (!move_uploaded_file($_FILES['avatar']['tmp_name'], '../' . $path)) {
$response = [
"status" => false,
"type" => 2,
"message" => "Ошибка при загрузке аватарки",
];
echo json_encode($response);
}
$password = md5($password);
mysqli_query($connect, "INSERT INTO `users` (`id`, `full_name`, `login`, `email`, `password`, `avatar`) VALUES (NULL, '$full_name', '$login', '$email', '$password', '$path')");
$response = [
"status" => true,
"message" => "Регистрация прошла успешно!",
];
echo json_encode($response);
} else {
$response = [
"status" => false,
"message" => "Пароли не совпадают",
];
echo json_encode($response);
}
?>
<?php
session_start();
require_once 'connect.php';
$login = $_POST['login'];
$password = $_POST['password'];
$error_fields = [];
if ($login === '') {
$error_fields[] = 'login';
}
if ($password === '') {
$error_fields[] = 'password';
}
if (!empty($error_fields)) {
$response = [
"status" => false,
"type" => 1,
"message" => "Проверьте правильность полей",
"fields" => $error_fields
];
echo json_encode($response);
die();
}
$password = md5($password);
$check_user = mysqli_query($connect, "SELECT * FROM `users` WHERE `login` = '$login' AND `password` = '$password'");
if (mysqli_num_rows($check_user) > 0) {
$user = mysqli_fetch_assoc($check_user);
$_SESSION['user'] = [
"id" => $user['id'],
"full_name" => $user['full_name'],
"avatar" => $user['avatar'],
"email" => $user['email']
];
$response = [
"status" => true
];
echo json_encode($response);
} else {
$response = [
"status" => false,
"message" => 'Не верный логин или пароль'
];
echo json_encode($response);
}
?>
<?php
session_start();
if (!$_SESSION['user']) {
header('Location: /');
}
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="assets/css/mainprof.css">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>
<!-- Профиль -->
<div class="conteiner">
<img src="<?= $_SESSION['user']['avatar'] ?>" width="200" alt="" class="ava">
<span class="log"><?= $_SESSION['user']['full_name'] ?></span>
<div class="menu">
<a href="#"class="menu-btn">Найти пользователя
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="mx-3" role="img" viewBox="0 0 24 24" focusable="false"><title>Поиск</title><circle cx="10.5" cy="10.5" r="7.5"></circle><path d="M21 21l-5.2-5.2"></path></svg></a>
<a href="#"class="menu-btn">Доступные доски</a>
<a href="vendor/logout.php" class="logout">Выход</a>
</div>
</div>
</div>
Подключение к доске
<code lang="php">
<?php
session_start();
require_once 'connect.php';
$task = $_GET['task'];
if ($task ==''){
echo 'Введите текст';
exit();
}
mysqli_query($connect, "INSERT INTO `mydata` (`id`, `task`) VALUES (NULL, '$task')");
header('Location:../profile.php')
?>
</code>
<!--Доска-->
<div class="conteiner2">
<div class="to-do">
<form action="vendor/addtodo.php" method="GET">
<input type="text" name="task" placeholder="Введите текст!">
<button type="submit"class="dob">Добавить </button>
</form>
<!--<div class="problem">
Какой-то текст
</div>-->
</div>
</div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Output all "task" from the database where by user id
SELECT `task` FROM `mydata` WHERE `user_id` = ?
$tasks = mysqli_query($connect, "SELECT `task` FROM `mydata` WHERE `user_id` =".$_SESSION['user']['id']);
if (mysqli_num_rows($tasks) > 0) {
while($user = mysqli_fetch_assoc($result)) {
echo $result['task'];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question