Delete from DB option only for logged in users

Hello All,

Struck with a code here. Here is my code that fetches all the post from database. I have a deleteform with it. I want that this delete option should appear with those status only where loggedin user is the author of those status posts, For rest he shouldnot see delete sign. But my script is not doing so, it is showing delete with each post.

<?php
{
	$myspeak="";$deleteform="";
	$sql="SELECT * FROM sayitstatus WHERE type='a' ORDER by posttime DESC";
	$query=mysqli_query($db_conx,$sql);
	$statusnumrows = mysqli_num_rows($query);
	if($statusnumrows>1){
	while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
	$id = $row["id"];
	$author = $row["author"];$data = $row["data"];
		if($author==$log_username)
		{
          $deleteform = '<form style="float:left;" method="post">';	
          $deleteform .='<input type="hidden" name="id" value='.$id.' >';
          $deleteform .='<input name="del" type="submit" title="Delete this Speak" value="X">';
          $deleteform .= '</form>';
		}
	$myspeak .='<div style="background-color:#fff;color:grey; font-size:16px; width:100%; margin-bottom:5px;overflow:hidden;"> <div><a style="color:grey" href="temp.php?u='.$author.'">'.$author.'</a></div><div style="margin-left:9%;">'.$data.'</div>'.$deleteform.'</div>';}
 echo $myspeak;
}}?>

Thanks
Shail

You need an else statement on that if ($author == $log_username), otherwise every record after the first that has that user as the author will see the link

So, you need to add right before the $myspeak .= line

else {
    $deleteform = '';
}
1 Like

Was working on this from last one hour and wasnot getting the solution…
You solved my problem in just ten minutes, thanks genius.