[FIXED] I fixed my problem. As suggested by Fred -ii- I visited this Q&A on SO. I saw he used checkboxes which to seems much more useful than buttons since you can take multiple objects out at a time. Also he attached the id of the object to the button like so just like Subin suggested as well.
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?php echo row['id']; ?>'/>
</form>
Here is the fixed code. I am now able to delete the boxes individually. Thank you for all the suggestions. I am now using mysqli as well.
$query = mysqli_query($connect, "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysqli_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b><a href="">'.$date2.'</a></div>';
echo '<div class="news-body">'.$post2.'</div>';
if (isset($_SESSION['username']) && ($_SESSION['level'] >= 3 || $_SESSION['group'] == 'Admin')) {
?>
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button[]' value="<?php echo $row['id']; ?>"/>
</form>
<?php
}
echo '<br>';
}
if(isset($_POST['delete_button'])) {
$boxid = $_POST['delete_button'];
for($i=0;$i<count($boxid);$i++){
$del_id = $boxid[$i];
mysqli_query($connect, "DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$del_id'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
}
}
[QUESTION] So currently I've been working on a website just for my learning purposes and Google thus far has been good help. Although, I can't seem to figure this problem out. I have a "news feed"
https://i.stack.imgur.com/oieXJ.png
it doesn't let me post images but its the only visual idea that I can give. Unless you want to visit my main page http://yuriah.net the news feed is just there without the delete buttons of course.
I want to add a "delete" button to each post so that I can delete each of them individually when I want. I am having problems with the current code im using. It deletes all of them from the database but I only want to delete the "post" that I click delete. Here is my source:
$query = mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysql_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
$boxid = $row['id'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b><a href="">'.$date2.'</a></div>';
echo '<div class="news-body">'.$post2.'</div>';
if ($_SESSION['level'] > 3 || $_SESSION['group'] == 'Admin' || $_SESSION['group'] == 'Owner') {
?>
<form action="" method='post'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='Delete' />
</form>
<?php
if(isset($_POST['delete_button'])) {
$con = mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
header('refresh:1; url=/');
mysql_close($con);
}
}
echo '<br>';
}
I am fairly new at PHP HTML CSS MYSQL etc. I am open to all suggestions and comments. Any help will be appreciated thank you.