-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload_image.php
104 lines (82 loc) · 4 KB
/
upload_image.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html>
<head>
<title>Signup</title>
<?php include_once "partials/meta.php"; ?>
<link rel="stylesheet" type="text/css" href="styles/signup.css">
</head>
<body>
<!-- Navbar partial -->
<!-- I got tired of copying and pasting the code for the navbar -->
<?php include_once "partials/navbar.php"; ?>
<main class="signup">
<div class="container">
<header>
<h1 class="font-serif"> Upload image </h1>
</header>
<?php
include_once "partials/db.php";
# Only run if the form is submitted and a user is signed in
if (isset($_COOKIE["user_id"]) && $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["name"]) && isset($_POST["url"])) {
try {
# Create database connection
$pdo = new PDO(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
# Insert the image info. into the `images` table
$sql = "INSERT INTO images (name, description, url, likes, user_id) VALUES (:name, :description, :url, :likes, :user_id)";
# Begin Transaction
$pdo->beginTransaction();
$statement = $pdo->prepare($sql);
# Use image info. from the form in sql query
$statement->bindValue(':name', $_POST['name']);
$statement->bindValue(':description', $_POST['description']);
$statement->bindValue(':url', $_POST['url']);
$statement->bindValue(':likes', $_POST['likes']);
$statement->bindValue(':user_id', $_COOKIE["user_id"]);
# Run prepared sql query
$statement->execute();
# Commit Transaction
$pdo->commit();
# Garbage collect the statement
$statement = null;
# Success
echo "<h2 align='center' class=\"text-center success\">Image Uploaded Successfully.</h2>";
} catch (PDOException $e) {
$pdo->rollback();
throw $e;
}
# Disconnect from database
$pdo = null;
} catch (PDOException $e) {
die($e->getMessage());
}
}
?>
<!-- Using post so signup information is not present in the URL -->
<form class="upload-image-form" method="POST">
<fieldset>
<input type="text" name="name" required />
<label for="name">Name</label>
</fieldset>
<fieldset>
<input type="text" name="description" required />
<label for="description">Description</label>
</fieldset>
<fieldset>
<input type="text" name="tags" required />
<label for="tags">Tags</label>
</fieldset>
<button id="upload_widget" class="cloudinary-button" type="button">
Upload Image
</button>
</form>
<div class="uploaded_images grid"></div>
</div>
</main>
<!-- Scripts for cloudinary. Cloudinary handles the image upload and storage -->
<script src="https://upload-widget.cloudinary.com/global/all.js" type="text/javascript"></script>
<script src="scripts/main.js" type="text/javascript" defer></script>
<?php include_once "partials/scripts.php"; ?>
</body>
</html>