-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlike_image.php
More file actions
49 lines (38 loc) · 1.62 KB
/
like_image.php
File metadata and controls
49 lines (38 loc) · 1.62 KB
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
<?php
include_once "partials/db.php";
# Only run if the form is submitted and a user is signed in
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["image_id"]) && isset($_POST["likes"])) {
try {
# Create database connection
$pdo = new PDO(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
# Update the image info. in the `images` table, add the new value to likes
$sql = "UPDATE images SET likes=:likes WHERE id=:id";
# Begin Transaction
$pdo->beginTransaction();
$statement = $pdo->prepare($sql);
# Increment likes
$likes = $_POST['likes'] + 1;
# Use image info. from the form in sql query
$statement->bindValue(':id', $_POST['image_id']);
$statement->bindValue(':likes', $_POST['likes'] + 1);
# Run prepared sql query
$statement->execute();
# Commit Transaction
$pdo->commit();
# Garbage collect the statement
$statement = null;
# Success
echo "<h2 class=\"text-center success\">Survey Completed Successfully.</h2>";
} catch (PDOException $e) {
$pdo->rollback();
throw $e;
}
# Disconnect from database
$pdo = null;
} catch (PDOException $e) {
die($e->getMessage());
}
}
?>