Repository navigation
Expand file tree
/
Copy pathIndex.php
More file actions
79 lines (73 loc) · 2.04 KB
/
Copy pathIndex.php
File metadata and controls
79 lines (73 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
// initialize errors variable
$errors = "";
//database info
$host = 'localhost';
$dbuser ='root'; // database admin
$dbpassword = ''; //database password
$dbname = 'todolist'; // database name
// connect to database
$db = mysqli_connect($host, $dbuser, $dbpassword, $dbname);
// insert a quote if submit button is clicked
if (isset($_POST['submit'])) { // if submit button is clicked
if (empty($_POST['task'])) { //if task line is empty
$errors = "You must fill in the task";
}
else{
$task = $_POST['task'];
$sql = "INSERT INTO tasks (task) VALUES ('$task')";
mysqli_query($db, $sql);
header('location: index.php');
}
}
// task delete
if (isset($_GET['del_task'])) {
$id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE id=".$id);
header('location: index.php');
}
?>
<!--html start-->
<!DOCTYPE html>
<html>
<head>
<title>Final</title>
<link rel="stylesheet" type="text/css" href="style.css"> <!---->
</head>
<body>
<body style="background-color:#293132;">
<div class="heading">
<h2 >ToDo List</h2>
</div>
<form method="post" action="index.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" id="add_btn" class="add_btn">Add Task</button>
</form>
<table>
<thead>
<tr>
<th>N</th>
<th>Tasks</th>
<th style="width: 60px;">Action</th>
</tr>
</thead>
<tbody>
<?php
// select all tasks if page is visited or refreshed
$tasks = mysqli_query($db, "SELECT * FROM tasks");
$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="index.php?del_task=<?php echo $row['id'] ?>">x</a>
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</body>
</html>