I'm creating a todo list to add tasks, delete tasks and set the priority of each task. However, I keep getting this error:
The main problem seems to be with the while loop on line 57 but I'll put all my code on incase the root of the issue is farther up.
<?php
//Creating variables for connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb";
//Create connection
$connection = mysqli_connect($servername,$username, $password, $database);
//Check connection
if(!$connection){ //if the connection failed
die("Connection failed:" . mysqli_connect_error());
}
echo "Connected successfully! </br>"; // if it connected succesfully
if(isset($_POST['submit']) ){ //if the submit button is clicked
$name = $_POST['name']; //Creating variable $name constaining the task entered
$priority = $_POST['priority']; //Creating variable $priority constaining the priority entered
$stmt = mysqli_prepare($connection, "INSERT INTO todos (name, priority) VALUES(?, ?)"); //Inserting the 2 values into the todo table
mysqli_stmt_bind_param($stmt, 'si', $name, $priority); //setting the task and priority as the 2 values mentioned above
mysqli_stmt_execute($stmt); //executing the above
}elseif(isset($_POST['delete'])){ //if the delete button is clicked
$id = $_POST['id']; //Creating variable $id constaining the id of the task entered
$stmt = mysqli_prepare($connection, "DELETE FROM todos WHERE id = ?"); //deleting the task containing an ____ id
mysqli_stmt_bind_param($stmt, 'i', $id); // setting the above ___ id to the id of the task wanting to be deleted
mysqli_stmt_execute($stmt); //executing the above
}
?>
<!DOCTYPE HTML> <!--Starting html part-->
<html lang="en"> <!--Setting page language to english-->
<head>
<title>Todo List</title> <!--title of webpage-->
<!--Importing the required fonts for the page-->
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
<link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css">
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
</head>
<body class="container">
<h1>Todo List</h1> <!--Title at top of page-->
<form method="POST" action=""> <!--Creating a form thats data is going to be sent by POST method-->
<input type="text" name="name" value=""> <!--Task bar-->
<label for="priority">Task Priority (between 1 and 3):</label> <!--Instructions for next input box-->
<input type="number" name="priority" min="1" max="3" style="width: 7em"> <!--Priority box from 1-3-->
<input type="submit" name="submit" value="Add"><!--Add button-->
</form>
<h2>Current Todos</h2> <!--Naming table below-->
<table class="table table-striped">
<thead><th>Priority</th><th>Task</th><th></th></thead> <!--Naming columns. third column header is blank because it's the delete button column-->
<tbody>
<?php
$stmt = mysqli_prepare($connection, "SELECT * FROM todos ORDER BY id DESC"); //Ordering table from 1st task entered to last
while ($row = mysqli_fetch_array($stmt)){ //Loops through all the tasks in the database
?>
<tr>
<td><?php echo $row['priority'] ?></td><!--printing each priority to the row -->
<td><?php echo $row['name'] ?></td> <!--printing each task to the row -->
<td>
<form method="POST"> <!--Creating a form thats data is going to be sent by POST method-->
<button type="submit" name="delete">Delete</button> <!--Makes the delete button-->
<input type="button" name="id" value="<?php echo $row ['id'] ?>">
<input type="hidden" name="delete" value="true">
</form>
</td>
</tr>
<?php
} //Closes while loop
?>
</tbody>
</table>
</body>
</html>