I am trying to make an Instagram like website in order to enhance my coding skills. I am echoing the image and a form with a button in it if the posted picture belongs to the logged-in user to delete the posted picture.
Here is the index.php:
<?php
require_once "includes/header.php";
?>
<?php
if (isset($_SESSION["sessionId"])){
echo "<br><br><br>";
$sql="SELECT * FROM post ORDER BY id DESC ";
$stmt=mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)){
echo "SQL HATASI. SIÇTIK";
}
else{
mysqli_stmt_execute($stmt);
$result= mysqli_stmt_get_result($stmt);
while ($row=mysqli_fetch_assoc($result)){ //this is where I show the user posted pictures.
$a= $row["file_name"];
$b=$row["username"];
$filepath="file_upload/upload/$a"; //this is where the uploaded pictures are stored.
$postID=$row["id"];
if ($row["username"]==$_SESSION["sessionUser"]){
echo "<img src='$filepath' width='60%' height='40%'>"."<br>"."This picture was posted by ". $b ."<form action='file_upload/delete.php?postid=$postID' method= 'GET'><button type='submit' name='delete'>delete </button></form>".$postID."<hr>";
}
else{
echo "<img src='$filepath' width='60%' height='40%'>"."<br>"."This picture was posted by ". $b ."<hr>";
}
}
}
}
else{
echo "Lütfen giriş yapın.";
require_once "includes/footer.php";
}
?>
What I am trying to do with the:
if ($row["username"]==$_SESSION["sessionUser"]){
echo "<img src='$filepath' width='60%' height='40%'>"."<br>"."This picture was posted by ". $b ."<form action='file_upload/delete.php?postid=$postID' method= 'GET'><button type='submit' name='delete'>delete </button></form>".$postID."<hr>";
}
Part is, I check whether the logged-in user posted that picture by checking if the username from the database equals to the username which is set by the super global SESSION.
Then I am using "<form action='file_upload/delete.php?postid=$postID' and triggering the upload.php while setting the $_GET["postid"]=$postID. What $postID is basically the auto incremented ID which is set while uploading the file and inserting the file name to the database. (Database consists of 3 columns: ID, file_name and username)
I am trying to get the information of which one of the delete buttons is pressed so that I can use that information while I am sending a query in the delete.php thus deleting the correct picture. I am trying to get the information of the ID of the post.
Here is how I am using that information in the delete.php:
<?php
session_start();
require_once "../includes/database.php";
if (isset($_GET["delete"])){
$postid=$_GET["postid"];
$sql="DELETE FROM post WHERE id=?";
$stmt=mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt,$sql)){
header("Location: ../index.php?sqlerror");
exit();
}
else if (empty($postid)){
header("Location: ../index.php?didntwork");
exit();
}
else{
mysqli_stmt_bind_param($stmt,"s",$postid);
mysqli_stmt_execute($stmt);
unset($_GET["postid"]);
header("Location: ../index.php?deletesuccess&$postid");
exit();
}
unset($_GET["postid"]);
}
However, it keeps me redirecting to ../index.php?didntwork because the $postid variable is empty, even though I set it to $postid=$_GET["postid"]. This means I can not get the information of the ID of the post when the user clicks to the delete button, in other words the "<form action='file_upload/delete.php?postid=$postID' part is not working the way I want.
How can I get the information, and is there another way to determine which one of the delete buttons is pressed? I am uploading the image of how the index.php looks like: here is the image