/
api_add_english_sentence.php
68 lines (57 loc) · 1.5 KB
/
api_add_english_sentence.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
<?php
require_once "conn.php";
header('Content-type:application/json;charset=utf-8'); //要輸出 json 記得要加這行 header,瀏覽器才會知道他是 json 格式的資料
header('Access-Control-Allow-Origin: *');
if (empty($_POST['username']) ||
empty($_POST['zh']) ||
empty($_POST['en'])) {
$json = array(
"ok" => false,
"message" => "Please input missing fields",
);
$response = json_encode($json); //變成 json 的格式
echo $response;
die();
}
$username = $_POST['username'];
$zh = $_POST['zh'];
$en = $_POST['en'];
$sql = "INSERT INTO daily_english(username, zh, en) VALUES (?, ?, ?) ";
$stmt = $conn->prepare($sql);
$stmt->bind_param('sss', $username, $zh, $en);
$result = $stmt->execute();
if (!$result) {
$json = array(
"ok" => false,
"message" => $conn->error,
);
$response = json_encode($json);
echo $response;
die();
}
$sql = "SELECT * FROM daily_english WHERE id = @@IDENTITY";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
$result = $stmt->get_result();
$dailyEnglish = array();
$row = $result->fetch_assoc();
array_push($dailyEnglish, array(
"id" => $row["id"],
"zh" => $row["zh"],
"en" => $row["en"],
));
if (!$result) {
$json = array(
"ok" => false,
"message" => $conn->error,
);
$response = json_encode($json);
echo $response;
die();
}
$json = array(
"ok" => true,
"dailyEnglish" => $dailyEnglish,
);
$response = json_encode($json);
echo $response;