<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./main_list.css">
<link href="https://fonts.googleapis.com/css2?family=PT+Sans&display=swap" rel="stylesheet">
<script src="./add_task.js" defer></script>
<title>Main List</title>
</head>
<body>
<div id="container">
<button id="buttonMainList"><a href="./logout.php">Logout</a></button>
<p id="userNameHere"><?php echo $_SESSION['name'] ?></p>
</div>
<h1 id="inform">TO DO LIST</h1>
<div id="add_task_form">
<button id="add_task">Add New Task</button>
<form id="add_form" name="task_form" method="post">
<div id="title_form">
<label for="title">Title</label>
<input type="text" name="title" id="title"><br>
</div>
<div id="description_form">
<label for="description">Description</label>
<input type="text" name="description" id="description"><br>
</div>
<div id="due_date_form">
<label for="due_date">Due Date</label>
<input type="text" name="due_date" id="due_date" placeholder="yyyy-mm-dd"><br>
</div>
<div id="time_form">
<label for="time">Time</label>
<input type="text" name="time" id="time" placeholder="hh:mi"><br>
</div>
<input id="submit_add_form" type="submit" name="submit" value="Save">
</form>
<br>
</div>
<table border="3" cellpadding="5" align="center">
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Due Date</th>
<th>Time</th>
</tr>
<?php foreach($allTask as $value){?>
<tr>
<td><?php echo $value['id'];?></td>
<td><?php echo $value['title'];?></td>
<td><?php echo $value['description'];?></td>
<td><?php echo $value['due_date'];?></td>
<td><?php echo $value['time'];?></td>
</tr>
<?php
}
?>
</body>
</html>
I have a problem. I want to display (fetch) all the tasks related to the user and put it into table format. But I don't know why the code generates an error said that:
Notice: Undefined variable: allTask in /opt/lampp/htdocs/list-32/main_list.php on line 82
Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/list-32/main_list.php on line 82
Can someone help me to fix the error?
<?PHP
session_start();
require 'connect.php';
if (isset($_POST['submit'])) {
$owner = $_SESSION['name'];
$title=$_POST['title'];
$description=$_POST['description'];
$due_date=$_POST['due_date'];
$time=$_POST['time'];
$state=0;
$insertQuery="INSERT INTO tasks (owner, title, description, due_date, time, state)
VALUES (:owner, :title, :description, :due_date, :time, :state)";
$preparedInsertStatement = $conn->prepare($insertQuery);
$preparedInsertStatement->bindValue(':owner', $owner);
$preparedInsertStatement->bindValue(':title', $title);
$preparedInsertStatement->bindValue(':description', $description);
$preparedInsertStatement->bindValue(':due_date', $due_date);
$preparedInsertStatement->bindValue(':time', $time);
$preparedInsertStatement->bindValue(':state', $state);
$valueInsert=$preparedInsertStatement->execute();
if($valueInsert){
echo 'Insert is done';
}
else{
echo 'Insert is not successful';
}
$displayQuery="SELECT * FROM tasks where owner=':owner'";
$displayTask= $conn->prepare($displayQuery);
$displayTask->bindValue(':owner', $owner);
$allTask=$displayTask->execute();
}
?>