This repository has been archived by the owner on Aug 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.Label.php
88 lines (81 loc) · 2.79 KB
/
crud.Label.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
include "conn.php";
include "class.Label.php";
$conn->query("SET NAMES utf8");
$success = true;
$html = "";
if ($_POST["action"] == "create"){
try {
$stmt = $conn->prepare('INSERT INTO labels (talk_id, title, start_at, detail) VALUES(:talk_id, :title, :start_at, :detail)');
$stmt->bindParam(':talk_id', $_POST["talk_id"], PDO::PARAM_INT);
$stmt->bindParam(':title', $_POST["title"], PDO::PARAM_STR);
$stmt->bindParam(':start_at', $_POST["start_at"], PDO::PARAM_STR);
$stmt->bindParam(':detail', $_POST["detail"], PDO::PARAM_STR);
$stmt->execute();
if ($stmt->rowCount() != 1)
$success = false;
else {
$stmt2 = $conn->prepare("SELECT * from labels WHERE id=:id");
$stmt2->setFetchMode(PDO::FETCH_CLASS, "Label");
$stmt2->execute(array('id' => $conn->lastInsertId()));
while($obj = $stmt2->fetch()) {
$label = $obj;
}
$html = $label->getAdminHTML();
$json = array(
"id"=>$label->id,
"start_at"=>$label->start_at,
"html"=>$html,
"success"=>$success
);
}
} catch(PDOException $e) {
$success = false;
file_put_contents('log/database.log', date("Y-m-d H:i:s") . " " . $e->getMessage() . "\n", FILE_APPEND);
}
} else if ($_POST["action"] == "update"){
try {
$stmt = $conn->prepare('UPDATE labels SET title=:title, start_at=:start_at, detail=:detail WHERE id=:id');
$stmt->bindParam(':id', $_POST["id"], PDO::PARAM_INT);
$stmt->bindParam(':title', $_POST["title"], PDO::PARAM_STR);
$stmt->bindParam(':start_at', $_POST["start_at"], PDO::PARAM_STR);
$stmt->bindParam(':detail', $_POST["detail"], PDO::PARAM_STR);
$stmt->execute();
// not checking for rowCount == 1, because if duplicate info was inserted for save, it will return 0.
$stmt2 = $conn->prepare("SELECT * from labels WHERE id=:id");
$stmt2->setFetchMode(PDO::FETCH_CLASS, "Label");
$stmt2->execute(array('id' => $_POST["id"]));
while($obj = $stmt2->fetch()) {
$label = $obj;
}
$html = $label->getAdminHTML();
$json = array(
"id"=>$label->id,
"start_at"=>$label->start_at,
"html"=>$html,
"success"=>$success
);
} catch(PDOException $e) {
$success = false;
file_put_contents('log/database.log', date("Y-m-d H:i:s") . " " . $e->getMessage() . "\n", FILE_APPEND);
}
} else if ($_POST["action"] == "delete"){
try {
$stmt = $conn->prepare('DELETE FROM labels WHERE id=:id');
$stmt->execute(array(
'id' => $_POST["id"]
));
if ($stmt->rowCount() != 1)
$success = false;
else {
$json = array(
"success"=>$success
);
}
} catch(PDOException $e) {
$success = false;
file_put_contents('log/database.log', date("Y-m-d H:i:s") . " " . $e->getMessage() . "\n", FILE_APPEND);
}
}
echo json_encode($json);
?>