-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
74 lines (64 loc) · 1.81 KB
/
server.php
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
<?php
//declaring errors i.e dont accept empty todo
$errors="";
//connect to database
$username="root";
$password="";
$database="to-do";
$host="localhost";
//create connection
$db=mysqli_connect($host,$username,$password,$database);
//check connection
if($db->connect_error){
die("connection failed:".$db->connect_error);
}
//getting data from form if submitted
if(isset($_POST['submit'])){
//assign todo to a variable task
$task = $_POST['task'];
//if todo is blank then return an error
if(empty($task)){
$errors = "You must fill in the task";
}else{
//query to add info to db
$query="INSERT INTO tasks(task) VALUES('$task')";
//run query
mysqli_query($db, $query);
//redirect user to same page
header('location:index.php');
}
}
//deleting a todo
if(isset($_GET['del_task'])){
$id = $_GET['del_task'];
//Delete query
$delete_query = "DELETE FROM tasks WHERE id=$id";
//run delete query
mysqli_query($db, $delete_query);
//redirect to same page
header('location:index.php');
}
// Checking to do
if(isset($_GET['check_task'])){
$checktask=$_GET['check_task'];
$true=1;
//update query
$update_query="UPDATE tasks SET Checked=$true WHERE id=$checktask";
//run update query
mysqli_query($db, $update_query);
//redirect to updated page
header('location:index.php');
}
// if(!isset($_GET['check_task'])){
// $checktask=$_GET['check_task'];
// $false=0;
// //update query
// $update_query="UPDATE tasks SET Checked=$false WHERE id = $checktask";
// //update db
// mysqli_query($db, $update_query);
// //redirect
// //header('location:index.php');
// }
//get all tasks from db to display
$tasks=mysqli_query($db, "SELECT * FROM tasks");
?>