forked from rpiambulance/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.signup_event.php
71 lines (56 loc) · 2.11 KB
/
.signup_event.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
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
require_once ".db_config.php";
$connection = new PDO("mysql:host=$dhost;dbname=$dname", $duser, $dpassword);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!isset($dname)) {
$dname = 'ambulanc_web';
}
if(!isset($_POST['event_id'])) {
return;
}
include ".functions.php";
$user = getUser($_POST['session_id'], $connection);
$username = $user['username'];
$eventId = $_POST['event_id'];
$statement = $connection->prepare("SELECT * FROM members WHERE username=:username");
$statement->bindParam(':username', $username);
$statement->execute();
$memberInfo = $statement->fetchAll(PDO::FETCH_ASSOC)[0];
$memberId = $memberInfo['id'];
$sql = "SELECT COUNT(*) AS count FROM events_attendees WHERE memberid = :memberId AND eventid = :eventId";
$statement=$connection->prepare($sql);
$statement->bindValue(':eventId', $eventId);
$statement->bindValue(':memberId', $memberId);
$statement->execute();
$alreadySignedUp = ($statement->fetchAll(PDO::FETCH_ASSOC)[0]['count'] > 0);
if($alreadySignedUp) {
return;
}
$sql = "SELECT COUNT(id) AS `count` FROM events_attendees WHERE eventid = :eventId GROUP BY eventid";
$statement=$connection->prepare($sql);
$statement->bindValue(':eventId', $eventId);
$statement->execute();
if($statement->rowCount() < 1) {
$currentCount = 0;
} else {
$currentCount = $statement->fetchAll(PDO::FETCH_ASSOC)[0]['count'];
}
$sql = "SELECT `limit` FROM events WHERE id = :eventId";
$statement=$connection->prepare($sql);
$statement->bindValue(':eventId', $eventId);
$statement->execute();
$limit = $statement->fetchAll(PDO::FETCH_ASSOC)[0]['limit'];
if($limit == '-1' || ($limit != '0' && $currentCount >= intval($limit))) {
return;
}
$sql = "INSERT INTO events_attendees
SELECT MAX(id) + 1 AS `id`, :eventId AS `eventid`, :memberId AS `memberid`
FROM events_attendees;
";
$statement=$connection->prepare($sql);
$statement->bindValue(':eventId', $eventId);
$statement->bindValue(':memberId', $memberId);
$statement->execute();
}
?>