· 6 years ago · Nov 26, 2019, 06:30 PM
1 <?php
2 header('Access-Control-Allow-Origin: *');
3 header('Content-Type: application/json');
4 require_once "connection.php";
5 if($_SERVER['REQUEST_METHOD']=="GET"){
6 $testStudent = array(
7 "name" => 'test',
8 'math' => 0,
9 'phis' => 0,
10 'prog' => 0,
11 'phisical' => 0,
12 'progress' => 0,
13 );
14 $testStudentObj = (object)$testStudent;
15 $createTableSql = "CREATE TABLE IF NOT EXISTS ` studentsv1` (`student_name` varchar(255),`math` int(3) DEFAULT '0',`phis` int(3) DEFAULT '0',`prog` int(3) DEFAULT '0',`phisical` int(3) DEFAULT '0',`progress` int(3) DEFAULT '0')";
16 $insertTestData = "INSERT INTO studentsv1 (`student_name`, `math`,`phis`,`prog`,`phisical`,`progress`) VALUES ('name',0,0,0,0,0)";
17 $stmt1=$pdo->prepare($createTableSql);
18 // $pdo->query($insertTestData);
19 $stmt1->execute();
20
21 $pdo->execute($insertTestData);
22 $selectAllSql = "SELECT * FROM `studentsv1` ";
23 $stmt1 =$pdo->prepare($selectAllSql);
24 $stmt1->execute();
25 $rows = $stmt1->fetchAll(PDO::FETCH_ASSOC);
26 echo json_encode($rows);
27 http_response_code(200);
28 }
29 elseif($_SERVER['REQUEST_METHOD']=="POST"){
30 $entityBody = file_get_contents('php://input');
31 $decodeBody = json_decode($entityBody,true);
32 // var_dump($decodeBody);
33 $progress = ($decodeBody->math + $decodeBody->phis + $decodeBody->prog + $decodeBody->phisical)/(count((array)$decodeBody) - 1);
34 $response = "Student:$decodeBody->name created";
35 echo $response = json_encode($response);
36 $sql = "INSERT INTO studentsv1(student_name,math,phis,prog,phisical,progress)
37 VALUES (:sname,:math,:phis,:prog,:physical,:progress)";
38 $stmt=$pdo->prepare($sql);
39 $lol = 43;
40 $stmt->execute([
41 'sname' => $decodeBody->name,
42 'math' => $decodeBody->math,
43 'phis' => $decodeBody->phis,
44 'prog' => $decodeBody->prog,
45 'physical' => $decodeBody->phisical,
46 'progress' => $progress,
47 ]);
48 }