-
Notifications
You must be signed in to change notification settings - Fork 0
/
addComment.php
executable file
·62 lines (58 loc) · 1.64 KB
/
addComment.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
<?php
if (!isset($_COOKIE["cur_user"]) or !isset($_POST["courseID"])) {
echo <<<EOF
<script>
alert("You need to log in!");
url="index.php";
window.location.href=url;
</script>
EOF;
// 登陆没
} else {
setcookie("cur_user", $_COOKIE["cur_user"], time() + 3600);
setcookie("cur_sid", $_COOKIE["cur_sid"], time() + 3600);
date_default_timezone_set("PRC");
$send_date = date("G:i:s,`m-d,`Y");
$username = $_COOKIE["cur_user"];
//对对时间和人名
$dsn = sprintf('mysql:host=localhost;dbname=main;charset=utf8;port=3306');
$userNaame = "root";
$password = "yry001223";
$conn = new PDO($dsn,$userNaame, $password);
//连接数据库
$cid = $_POST["courseID"];
$sid = $_COOKIE["cur_sid"];
$text = $_POST["comment-text"];
//读取数据
$message_length = strlen($text);
$buffer_size = 500;
// 看看你评论写了多少字,按行插入
if ($message_length % $buffer_size == 0) {
$total_index = (int)($message_length / $buffer_size);
} else {
$total_index = (int)($message_length / $buffer_size) + 1;
}
$sql_command = <<<EOF
insert into comment values (?, ?, ?, ?, ?, ?)
-- 插入评论一个一个来
EOF;
$sql_run = $conn->prepare($sql_command);
for ($i=1; $i<=$total_index; $i++) {
$sql_run->execute([
$cid,
$sid,
$send_date,
substr($text, 0, $buffer_size),
$i,
$total_index
]);
$text = substr($text, $buffer_size);
}
$conn = null;
echo <<<EOF
<script>
<!--跳到对应的课程界面-->
window.location.href="CourseInfo.php?courseID=$cid"
</script>
EOF;
}