-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_task.php
60 lines (49 loc) · 1.75 KB
/
create_task.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
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-Type: application/json');
/*
* Following code will create a new task row
* All task details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
$postdata = json_decode(file_get_contents("php://input"), true);
$name = $postdata['name'];
$description = $postdata['description'];
// check for required fields
//if (isset($_POST['name']) && isset($_POST['description'])) {
if (isset($name) && isset($description)) {
/*
$name = $_POST['name'];
$description = $_POST['description'];
*/
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysqli_query($db->connect(),"INSERT INTO tasks(name, description, dateCreated, dateUpdated) VALUES('$name', '$description', NOW(), NOW())");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Task successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>