Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP MySQL Removing row from database through a delete button attached to an "object"
    text
    copied!<p><strong>[FIXED]</strong> I fixed my problem. As suggested by <a href="https://stackoverflow.com/users/1415724/fred-ii">Fred -ii-</a> I visited <a href="https://stackoverflow.com/questions/14475096/delete-multiple-rows-by-selecting-checkboxes-using-php">this Q&amp;A on SO</a>. 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 <a href="https://stackoverflow.com/users/1372424/subin">Subin</a> suggested as well.</p> <p><code>&lt;form action="" method='POST'&gt; &lt;input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='&lt;?php echo row['id']; ?&gt;'/&gt; &lt;/form&gt;</code></p> <p>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.</p> <pre><code>$query = mysqli_query($connect, "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('&lt;p id="formbox" style="text-align:center;"&gt;There was an unexpected error grabbing news from the database&lt;/p&gt;'); while ($row = mysqli_fetch_array($query)) { $title2 = $row['title']; $post2 = $row['post']; $date2 = $row['date']; $author = $row['author']; echo '&lt;div class="news-title"&gt;&lt;b style="float:left;"&gt;'.$author.'&lt;/b&gt;&lt;b style="text-align:center; color:green;"&gt;'.$title2.'&lt;/b&gt;&lt;a href=""&gt;'.$date2.'&lt;/a&gt;&lt;/div&gt;'; echo '&lt;div class="news-body"&gt;'.$post2.'&lt;/div&gt;'; if (isset($_SESSION['username']) &amp;&amp; ($_SESSION['level'] &gt;= 3 || $_SESSION['group'] == 'Admin')) { ?&gt; &lt;form action="" method='POST'&gt; &lt;input style='display:block; margin:0 auto;'type='submit' name='delete_button[]' value="&lt;?php echo $row['id']; ?&gt;"/&gt; &lt;/form&gt; &lt;?php } echo '&lt;br&gt;'; } if(isset($_POST['delete_button'])) { $boxid = $_POST['delete_button']; for($i=0;$i&lt;count($boxid);$i++){ $del_id = $boxid[$i]; mysqli_query($connect, "DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$del_id'") or die('&lt;p id="formbox" style="text-align:center;"&gt;There was an unexpected error deleting the post from the database&lt;/p&gt;'); } } </code></pre> <p><strong>[QUESTION]</strong> 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"</p> <p><a href="https://i.stack.imgur.com/oieXJ.png" rel="nofollow noreferrer">http://i.stack.imgur.com/oieXJ.png</a></p> <p>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 <a href="http://yuriah.net" rel="nofollow noreferrer">http://yuriah.net</a> the news feed is just there without the delete buttons of course.</p> <p>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:</p> <pre><code>$query = mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('&lt;p id="formbox" style="text-align:center;"&gt;There was an unexpected error grabbing news from the database&lt;/p&gt;'); while ($row = mysql_fetch_array($query)) { $title2 = $row['title']; $post2 = $row['post']; $date2 = $row['date']; $author = $row['author']; $boxid = $row['id']; echo '&lt;div class="news-title"&gt;&lt;b style="float:left;"&gt;'.$author.'&lt;/b&gt;&lt;b style="text-align:center; color:green;"&gt;'.$title2.'&lt;/b&gt;&lt;a href=""&gt;'.$date2.'&lt;/a&gt;&lt;/div&gt;'; echo '&lt;div class="news-body"&gt;'.$post2.'&lt;/div&gt;'; if ($_SESSION['level'] &gt; 3 || $_SESSION['group'] == 'Admin' || $_SESSION['group'] == 'Owner') { ?&gt; &lt;form action="" method='post'&gt; &lt;input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='Delete' /&gt; &lt;/form&gt; &lt;?php if(isset($_POST['delete_button'])) { $con = mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'") or die('&lt;p id="formbox" style="text-align:center;"&gt;There was an unexpected error deleting the post from the database&lt;/p&gt;'); header('refresh:1; url=/'); mysql_close($con); } } echo '&lt;br&gt;'; } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload