-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
66 lines (59 loc) · 1.81 KB
/
api.php
File metadata and controls
66 lines (59 loc) · 1.81 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
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
$servername = "localhost";
$username = "u374132832_godd";
$password = "I636lC7vZPol";
$dbname = "u374132832_godd";
$_POST = json_decode(trim(file_get_contents('php://input')), true);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['add-question'])){
$topic_id = $_POST['topic_id'];
$json = json_encode($_POST['json']);
$sql = "INSERT INTO questions (json, topic_id)
VALUES ($json, $topic_id)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo $conn->error;
}
}
if (isset($_POST['get-questions'])){
$topic_id = $_POST['topic_id'];
$sql = "SELECT * FROM questions where topic_id='$topic_id' ORDER BY RAND() LIMIT 7";
if ($result = $conn->query($sql)){
echo json_encode($result->fetch_all(MYSQLI_ASSOC));
}
}
if (isset($_GET['get'])){
$sql = "SELECT * FROM questions ";
if (isset($_GET['topic'])){
$topic_id = $_GET['topic'];
$sql = $sql . "where topic_id='$topic_id'";
}
$page_result = 10;
$offset = 0;
$page_value = 1;
if (isset($_GET['page'])){
$page_value = $_GET['page'];
}
if($page_value > 1)
{
$offset = ($page_value - 1) * $page_result;
}
$sql = $sql . " ORDER BY 1 DESC limit $offset, $page_result ";
if ($result = $conn->query($sql)){
echo json_encode($result->fetch_all(MYSQLI_ASSOC));
}
}
if (isset($_POST['delete'])){
$id = $_POST['id'];
$sql = "DELETE FROM questions where id='$id' ";
$conn->query($sql);
}
$conn->close();