I am trying to delete some videos. The videos are echo'd out individually with their own 'delete' input. The input value takes the id of the video, I didn't know of any other work around for that.
I'd like to delete the video that corresponds with the button pressed, if that makes sense. Anyways, I would like to delete the video where the video id is equal to the video id of the input or the value of the input pressed.
I am positive the way I am carrying this out is not correct, as I am thrown an 'index undefined error'.
Here is the disgusting code at hand. Recommendations are appreciated, but please try and keep answers in line with the relevant information given. Thank You.
<?php
error_reporting(-1);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require('dbcon/dbcon.php');
include('header.php');
include('user.php');
$channel_id = $_SESSION['channel_id'];
$query = $pdo->prepare("SELECT * FROM videos001 WHERE uploader = :channel_id");
$query->bindValue('channel_id', $_SESSION['channel_id']);
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$title = $row['video_title'];
$video_path = $row['video_path'];
$video_id = $row['video_id'];
/*
- get videos of user
- display videos out onto page
*/
?>
<html>
<body>
<div class="content">
<form method="post">
<h3><?php echo $title; ?></h3>
<input type="button" name="delete" id="delete" value="<?php echo $video_id;?>" onclick="deleteVideo()">
</form>
</div>
</body>
</html>
<?php }
function removeVideoFromFilesystem(PDO $pdo, $video_path, $video_id) {
//chdir($video_path);
//unlink($video_id . ".mp3");
$query = $pdo->prepare("DELETE from videos001 where video_id = :video_id");
$query->bindValue(':video_id', $_POST['delete']);
$query->execute();
}
if (isset($_POST['call_func'])) {
removeVideoFromFileSystem($pdo, $video_path, $_POST['delete']);
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function deleteVideo() {
$.ajax({
url: 'dashboard.php',
type: 'post',
data: {"call_func":"1"},
success: function(response) { console.log(response); }
});
}
</script>
</head>
<body><?php print_r($_POST['delete']); ?></body>
</html>