-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnew_question.php
90 lines (73 loc) · 3.02 KB
/
new_question.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
<?php
session_start();
require_once("connect.php");
if (!isset($_SESSION["username"]) || !isset($_SESSION["user_id"])) {
header("Location: login.php");
exit();
}
$topic_id = $_GET["topic_id"]; // Get the topic_id from the URL
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = $_POST["title"];
$content = $_POST["content"];
$user_id = $_SESSION["user_id"];
// Check if an image file was uploaded
$image_path = ""; // Default image path if no image was uploaded
if (isset($_FILES["image"]) && $_FILES["image"]["error"] === UPLOAD_ERR_OK) {
$image_dir = "post upload/"; // Directory to store uploaded images
$image_name = $_FILES["image"]["name"];
// Generate a unique file name by appending a timestamp
$timestamp = time();
$image_name = $timestamp . "_" . $image_name;
$image_path = $image_dir . $image_name;
if (!move_uploaded_file($_FILES["image"]["tmp_name"], $image_path)) {
echo "Error: Failed to move uploaded image.";
exit();
}
}
// Insert the new question into the database
$insert_query = "INSERT INTO questions (topic_id, title, content, user_id, image_path) VALUES (:topic_id, :title, :content, :user_id, :image_path)";
try {
$stmt = $pdo->prepare($insert_query);
$stmt->bindParam(':topic_id', $topic_id);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->bindParam(':user_id', $user_id);
$stmt->bindParam(':image_path', $image_path);
if ($stmt->execute()) {
$question_id = $pdo->lastInsertId(); // Get the question_id of the question that was just inserted
// Increment the question_count for the user
$update_count_query = "UPDATE user SET question_count = COALESCE(question_count, 0) + 1 WHERE id = :user_id";
$update_stmt = $pdo->prepare($update_count_query);
$update_stmt->bindParam(':user_id', $user_id);
$update_stmt->execute();
header("Location: question.php?id=$question_id");
exit();
} else {
echo "Error: Failed to insert question.";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>New Question</title>
<?php include("header.php"); ?>
</head>
<body>
<h1>New Question</h1>
<div class="container">
<form method="post" enctype="multipart/form-data">
<label for="title">Title:</label>
<input type="text" name="title" required><br>
<label for="content">Content:</label>
<textarea name="content" rows="4" required></textarea><br>
<label for="image">Upload Image:</label>
<input type="file" name="image" accept="image/*"><br>
<input type="submit" class="btn" name="submit" value="Post Question">
</form>
</div>
</body>
</html>