-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_post.php
41 lines (35 loc) · 1.32 KB
/
create_post.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
<?php
session_start();
include 'includes/DatabaseConnection.php';
include 'includes/DatabaseFunctions.php';
require_once 'includes/session.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$questiontitle = $_POST['questiontitle'] ?? '';
$questiontext = $_POST['questiontext'] ?? null;
$selectedModuleId = $_POST['selectedModuleId'] ?? null;
$postType = $_POST['postType'] ?? 'text';
$uploadedImage = null;
if ($postType === 'image' && isset($_FILES['questionimage']) && $_FILES['questionimage']['error'] === UPLOAD_ERR_OK) {
$uploadedImage = uploadImage($pdo, $_FILES['questionimage'], 'ques_uploads/');
if (!$uploadedImage) {
$error = 'Failed to upload the image.';
}
}
if (!$questiontitle || (!$questiontext && $postType !== 'image') || !$selectedModuleId) {
$error = "Please complete all required fields.";
} else {
try {
insertQuestion($pdo, $user_id, $questiontitle, $questiontext, $uploadedImage, $selectedModuleId);
header('Location: questions.php');
exit();
} catch (PDOException $e) {
$error = "An error occurred: " . $e->getMessage();
}
}
}
ob_start();
include 'templates/create_post.html.php';
$output = ob_get_clean();
include 'templates/layout.html.php';
?>