I'm trying to make a to-do-list that's user specific, meaning it shows your unique tasks depending on your user ID. The application works, but it doesn't assign the task to the current user when submitting. When I submit a task, the to-do-list stays empty, but when I check the database the task is submitted.
The problem here is that the user ID stays 0 when submitting the task, so it doesn't show it in 'my tasks' because the id isn't the same as the user who views it. My user ID is 2, the task should assign the task to user ID 2 when submitting or any other ID depending on the submitting user.
(I'm fairly new to coding, so my code might not be as clean as it can be)
--
<?php
$errors = "";
$db = mysqli_connect("localhost", "root", "", "certanet");
if (isset($_POST['submit'])) {
if (empty($_POST['task'])) {
$errors = "";
}else{
$task = $_POST['task'];
$sql = "INSERT INTO tasks (task) VALUES ('$task')";
mysqli_query($db, $sql);
header('location: dashboard.php');
}
}
if (isset($_GET['del_task'])) {
$id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE id=".$id);
header('location: dashboard.php');
}
?>
-- the application itself:
<form method="post" action="dashboard.php" class="input_form">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
<input type="text" name="task" class="task_input">
<button type="submit" name="submit">Toevoegen</button>
</form>
<table>
<tbody id="todo">
<?php
$tasks = mysqli_query($db, "SELECT * FROM tasks WHERE idUsers = '".$_SESSION["userId"]."' ORDER BY id DESC");
$i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<tr>
<td> <?php echo $i; ?> </td>
<td class="task"> <?php echo $row['task']; ?> </td>
<td class="delete">
<a href="dashboard.php?del_task=<?php echo $row['id'] ?>">x</a>
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>