Delete posts by post id

Hiho, I really need some help with this…

I have my index page where administrators can add new posts, but I also wish to give them the option of deleting or making changes to their posts.

I’m using two images as input elements and not sure how it works with the form… anyway, each post has a unique ID, just not sure how to connect it with the form. Essentially the images of the input element are only visible if the administrator is logged in, which means that guests won’t be able to see them.

I have the form inside the while loop… this might be the thing that’s confusing me???

The code below displays new posts and contains the form.
[php]

<?php $getnews = mysql_query("SELECT * FROM `news` ORDER BY id DESC") while ($row = mysql_fetch_assoc($getnews)) { $id = $row['id']; $title = $row['title']; $body = $row['body']; $date = $row['date']; $poster = $row['poster']; echo $title; ?> posted by <?php echo $poster; ?> on <?php echo $date; ?> <?php if(logged_in() === true) { ?>
        <div id="post">
          <form action="" method="POST">
	        <input type="image" name="delete" src="images/deletepost.png" class="" title="Delete Post"> 
           	<input type="image" name="edit" src="images/editpost.png" class="" title="Edit Post">
          </form>
       </div>							
<?php } ?>
 <?php echo nl2br($body); 

}
?>
[/php]

Not sure how I can get the wanted post ID

[php]
if(empty($_POST) === false) {

	$postID = $_POST['delete'];
	
	mysql_query("DELETE FROM `news` WHERE `id` = '$postID'");
} 

[/php]

Any help is appreciated.

<?php$getnews = mysql_query("SELECT * FROM `news` ORDER BY id DESC")while ($row = mysql_fetch_assoc($getnews)) {	$id = $row['id'];	$title = $row['title'];	$body = $row['body'];	$date = $row['date'];					$poster = $row['poster'];           echo $title; ?> posted by <?php echo $poster; ?> on <?php echo $date; ?><?php if(logged_in() === true) {?>            <div id="post">	          <form action="" method="POST">		        <input type="image" name="delete" src="images/deletepost.png" class="" title="Delete Post" value="<?php echo $id ?>> 	           	<input type="image" name="edit" src="images/editpost.png" class="" title="Edit Post">	          </form>           </div>							<?php } ?>     <?php echo nl2br($body); }?> 

[php]if(empty($_POST) === false) {
$postID = $_POST[‘delete’];
mysql_query(“DELETE FROM news WHERE id = ‘$postID’”);
} [/php]

See if that works. You will probably want to do more to validate this because if someone wanted to, they could easily start modifying and removing posts on your site by writing their own forms.

hmm almost…

[php]

<?php $getnews = mysql_query("SELECT * FROM `news` ORDER BY id DESC") while ($row = mysql_fetch_assoc($getnews)) { $id = $row['id']; $title = $row['title']; $body = $row['body']; $date = $row['date']; $poster = $row['poster']; echo $title; ?> posted by <?php echo $poster; ?> on <?php echo $date; ?>
	<?php if(logged_in() === true) { ?>            
		<div id="post">             
			<form action="" method="POST">              
				<input type="image" name="delete" src="images/deletepost.png" class="" title="Delete Post" value="<?php echo $id; ?>">                  
			</form>           
		</div>                     
	<?php } ?>     
<?php echo nl2br($body); } ?>

[/php]

[php]

<?php if(empty($_POST) === false) { $postID = $_POST['delete']; mysql_query("DELETE * FROM `news` WHERE `id` = '$postID'"); header('Location: index.php?success'); } ?>

[/php]

I get through to the index.php?success page but it doesn’t remove the post… so either the query is wrong or the $postID variable isn’t holding the post ID…

any ideas?

In a situation like that first two things you do is

[ol][li]echo the variable to see if it holds an ID[/li]
[li]chechk the query for errors sometimes query dont give error but they still dont work cuzz minor error[/li][/ol]

Thanks for the advise, I did what you said and it does gives me an SQL error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘* FROM news WHERE id = ‘’’ at line 1

I also tried to just echo the $postID but it didn’t output anything so I assume that’s why it’s not working. The variable isn’t connected to the input element… am I right?

yea you can say that. however the query error you getting doesn’t have anything to do with the ID variable not holding a value.

but the way to fix this would be to make sure that the $postID variable is actually holding the correct ID, and then run the query…

Any idea why it’s not picking up on the input element?
[php]
[/php]

[php]<?php
if(empty($_POST) === false) {
$postID = $_POST[‘delete’];
mysql_query(“DELETE * FROM news WHERE id = ‘$postID’”) or die(mysql_error());
header(‘Location: index.php?success’);
}
?>[/php]

[php]

<?php if(empty($_POST) === false) { echo "Good so far"; $postID = $_POST['delete']; mysql_query("DELETE * FROM `news` WHERE `id` = '$postID'") or die(mysql_error()); header('Location: index.php?success'); } ?>

[/php]

run the code above to see if the echo i typed is echoed, maybe the statement is not met

It’s not displaying “Good so far”

[php]

<?php if(empty($_POST) === false) { echo 'Good so far'; $postID = $_POST['delete']; echo $postID; header('Location: index.php?success'); } else { echo 'ok?'; } ?>

[/php]

This outputs the “ok?”. Before and after I click the submit image, how come it still redirects me to the index.php?success page when I only get “ok?” displayed?

you shouldnt be getting Ok?.
the problem here is not the statement
[php]
if(empty($_POST) === false) {
[/php]
the condition is not being met somehow

try these
[php]
if (isset($_POST[‘btnSubmit’]))
{
echo ‘Good so far’;
$postID = $_POST[‘delete’];
echo $postID;
header(‘Location: index.php?success’);

} else {
      echo 'ok?'; 
      }

[/php]

ON YOUR FORM CHANGE INOPUT Type = to sumbit not image and name the button “btnSubmit”

The form now looks like:
[php]

[/php]

and…
[php]

<?php if (isset($_POST['btnSubmit'])) { echo 'Good so far'; $postID = $_POST['btnSubmit']; echo $postID; mysql_query("DELETE * FROM `news` WHERE `id` = '$postID'"); header('Location: index.php?success'); } else { echo 'ok?'; } ?>

[/php]

It still only displays “ok?”

post your complete form and php code i will test it on my computer

dont include any senstitive infromation like password

[php]<?php
ob_start();
include ‘init.php’;

if (isset($_POST['delete'])) {
	$postID = $_POST['delete'];
	mysql_query("DELETE * FROM `news` WHERE `id` = '$postID'");
	header('Location: index.php?success');
} else {
	echo 'ok?'; 
  }

?>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
My title</title
<?php $getnews = mysql_query("SELECT * FROM `news` ORDER BY id DESC");
			while ($row = mysql_fetch_assoc($getnews)) {
				$id = $row['id'];
				$title = $row['title'];
				$body = $row['body'];
				$date = $row['date'];				
				$poster = $row['poster'];
		?> 
<?php echo $title; ?> posted by <?php echo $poster; ?> on <?php echo $date; ?> <?php if(logged_in() === true) { ?>
<div id="post">
   <form action="" method="POST">						 						
      <input type="submit" name="delete" title="Delete Post" value="<?php echo $id; ?>">
   </form>
</div>
<?php } ?>
<p><?php echo nl2br($body); ?></p> 
<?php } ?>
<?php ob_end_flush(); ?>[/php]

I excluded a lot in the head and body section, like css files, and other divs…

can you kindly export your table and post your sql table structure and at least 2 records for me to test it.

im getting too much errors because i dont have all the files needed. private message me your skype name i will help you thru teamviewer

Thanks so much man for helping me out Wilson!

you are very welcome i’m so glad that i could help

i will mark this thread solved!!

Sponsor our Newsletter | Privacy Policy | Terms of Service