-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_music.php
74 lines (61 loc) · 2.26 KB
/
delete_music.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
<?php
require_once "config.php";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
session_start();
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] === false) {
header("Location: login.php");
exit();
}
/*
* Display the users a form that they can use to delete the ratings of their
* music.
*/
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["Delete"])) {
$music_id = intval($_POST['id']);
$sql = "SELECT username, artist, song, rating FROM ratings WHERE id=$music_id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$music_username = $row['username'];
$music_artist = $row['artist'];
$music_song = $row['song'];
$music_rating = $row['rating'];
?>
<?php
}
else if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["Delete_Confirm"])) {
$sql = "DELETE FROM ratings WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "i", $_POST["id"]);
try {
if (mysqli_stmt_execute($stmt) === TRUE) {
header("Location: main.php");
exit();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
catch (Exception $e) {
echo $e;
}
}
else if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["Delete_Cancel"])) {
header("Location: main.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<body>
<!-- shows on top of the page that the user is logged in as -->
<h1>You are logged in <?php echo $_SESSION['username']; ?></h1>
<?php echo "Are you sure you want to delete this?" ?>
<form action="" method="post">
<input type='hidden' name='id' value=<?php echo $music_id ?>>
<input type="submit" value="No" name="Delete_Cancel">
<input type="submit" value="Yes" name="Delete_Confirm">
</form>
</body>
</html>