/
add.php
58 lines (51 loc) · 1.62 KB
/
add.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
<?php
include "base.php";
$url = $_POST["url"];
$tags = $_POST["tags"];
$userID = $_SESSION['UserID'];
echo $url;
echo $tags;
echo $userID;
function checkIfUserAlreadyHasThisUrl($url, $userID) {
$query = "SELECT * from links where url = ? and userID = ?";
$params = [$url, $userID];
$result = selectQuery($query, $params);
if (count($result) == 1) {
return 0;
} else {
return 1;
}
}
if (checkIfUserAlreadyHasThisUrl($url, $userID) == 0) {
echo "link already exists";
break;
} else {
$title = getPageTitle($url);
$query = "INSERT into links (userID, url, title, timestamp) VALUES (?, ?, ?, now())";
$params = [$userID, $url, $title];
$lastInserted = editQuery($query, $params);
if (strlen($tags) > 0) {
$tagString = strtolower($tags);
$tags = stringToArray($tagString);
$tags = array_unique($tags);
foreach($tags as $tag) {
$query = "SELECT * from tags WHERE tag = ?";
$params = [$tag];
$res = selectQuery($query, $params);
if (count($res) == 0) {
$query = "INSERT into tags (tag) VALUES (?)";
$params = [$tag];
editQuery($query, $params);
}
}
foreach($tags as $tag) {
$query = "SELECT tagID from tags WHERE tag = ?";
$params = [$tag];
$res = selectQuery($query, $params);
$tagID = $res[0]["tagID"];
$query = "INSERT into taggedlinks (tagID, linkID) VALUES (?, ?)";
$params = [$tagID, $lastInserted];
editQuery($query, $params);
}
}
}