So for the admin page of where "reviews" can be written and posted, I've created a delete article function but it's not one I wanted.

The one I have now works the following way...

A while loop prints out the specified information with their associated IDs.
After they are all printed, there is a box under them all prompting you to enter the id number of the comment to delete.
The number which is entered is posted to another page where it is found in the database and that specific comment is deleted.

I want to use a delete button under each comment instead. But I would need to post the value which is printed out and there isn't an option to post FROM php. Is there an obvious method which I'm not noticing maybe? some enlightenment would be good!


Hopefully my issue was understandable^^

Turn each loop into a form. Output the id as an input field on the form. Delete row when form is submitted.

Sorry, I don't follow. Here's what I currently do:

<?php
$con = mysql_connect("localhost","name","password");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db", $con);

$sql  = "SELECT id, name, comment, date FROM reviews ORDER BY id DESC LIMIT 20"; /*Return results in descending order, newest 20*/
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<div class="comments_top">
<?php
		 echo "{$row['comment']} <br><br>" ;	
?>
</div>
<font color="#e60000">
<?php
         echo "&nbsp&nbsp&nbsp&nbsp&nbsp{$row['name']} " ;	
?>
</font>
<font color="#8e8d8d">
<?php
	     echo "posted this on {$row['date']}.<br>" ;
		 echo "Id number: {$row['id']}.<br>" ;
?>
</font>
<br>
<?php
}
?>
</div>
<br><hr><br>
<strong>DELETE ID:</strong> <br>
<br>
<form method="post" action="delete_comment.php">
ID of comment to delete: <input name='id_to_delete' id='id_to_delete' type='text' maxlength='10'/>
<input type="submit" value="Delete"/>
Member Avatar for Zagga

Hi asif49,

Just move your delete code within the while loop

. . .

<font color="#8e8d8d">
<?php
echo "posted this on {$row['date']}.<br>" ;
echo "Id number: {$row['id']}.<br>" ;
$delete_id = $row['id'];
echo "</font>";
echo "<br><br><br>";
echo "<strong>DELETE:</strong><br><br>";
echo "<form method='post' action='delete_comment.php'>";
echo "<input name='id_to_delete' id='id_to_delete' value='$delete_id' type='hidden'/>";
echo "<input type='submit' value='Delete'/>";
echo "</form>";
} // End of WHILE loop
echo "</div>";

This will give you a delete button after each comment.

This will also work (by just changing a little bit of your own code)

$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<div class="comments_top">
<?php
		 echo "{$row['comment']} <br><br>" ;	
?>
</div>
<font color="#e60000">
<?php
         echo "&nbsp&nbsp&nbsp&nbsp&nbsp{$row['name']} " ;	
?>
</font>
<font color="#8e8d8d">
<?php
	     echo "posted this on {$row['date']}.<br>" ;
		 echo "Id number: {$row['id']}.<br>" ;
?>
</font>
<br>

<br><hr><br>
<strong>DELETE ID:</strong> <br>
<br>
<form method="post" action="delete_comment.php">
<input name='id_to_delete' id='id_to_delete' value='<?php echo $row['id']; ?>' type='hidden' />
<input type="submit" value="Delete"/>
<?php
}
?>
</div>

Didn't try the first one because the second one was so easy to implement and I'm kind of lazy. It worked a treat aswell. Cheers!

Member Avatar for Zagga

Both methods have the same effect. vidjin's approach is probably better though as it doesn't change as much of your code as my approach. I put your HTML in echo statements to save breaking in and out of PHP, just my personal preference.

Glad it works for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.