A
A
Arthur6662020-04-15 21:29:39
PHP
Arthur666, 2020-04-15 21:29:39

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');
    }


Connecting to Registration
<?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);
}

?>


Connecting to Authorization

<?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);
}
?>


The board itself (profile)
<?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>


DB table user 5e9751d573605936289214.png
DB table mydata5e9751e628efc066397304.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2020-04-15
@dimonchik2013

little code

A
alex159, 2020-04-15
@alex159

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'];
    }
}

Escape all incoming data and use more modern methods of communicating with the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question