-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteExercise.php
More file actions
59 lines (46 loc) · 1.45 KB
/
deleteExercise.php
File metadata and controls
59 lines (46 loc) · 1.45 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
50
51
52
53
54
55
56
57
58
59
<?php
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
// Connect to database
require_once "db_config.php";
// Get url to deleted file
$sql_query = "SELECT filePath FROM homework WHERE id = ?";
if ($stmt = mysqli_prepare($db_connection, $sql_query)) {
// Bind variables to prepared SQL statement
mysqli_stmt_bind_param($stmt, 'i', $_GET['homeworkId']);
// Execute SQL statement
if (mysqli_stmt_execute($stmt)) {
$res = $stmt->get_result();
$row = $res ->fetch_assoc();
$deleteFilePath = $row['filePath'];
}
else {
echo "Cannot execute select SQL query";
exit;
}
mysqli_stmt_close($stmt);
}
// Delete from database
$sql_query = "DELETE FROM homework WHERE id = ? AND teacherId = ?";
if ($stmt = mysqli_prepare($db_connection, $sql_query)) {
// Bind variables to prepared SQL statement
mysqli_stmt_bind_param($stmt, 'ii', $_GET['homeworkId'], $_SESSION['id']);
// Execute SQL statement
if (mysqli_stmt_execute($stmt)) {
$res = $stmt->get_result();
// Remove file from folder uploads/
unlink($deleteFilePath);
}
else {
echo "Cannot execute delete SQL query";
exit;
}
mysqli_stmt_close($stmt);
}
mysqli_close($db_connection);
header('location: listExercise.php');
?>