Jump to content

Recommended Posts

I created a blog and what I am trying to delete a post when I click the delete button underneath the post. I have messed around with this for a while and can't seem to figure out why it is not working.

 

Here is my code on the post page...

 


include "connect_to_mysql.php"; 

$sql = "SELECT * FROM posts ORDER BY time_posted DESC";

$results = mysql_query($sql);


while($row = mysql_fetch_array($results)) {

  
   echo "</br>";
	echo  "</br>";

    echo $row['title'] ."</br>";  echo $row['date_posted'] . "<br />";
	echo $row['time_posted'] ."</br></br>";
	echo $row['content'] . "<br /></br>";
	echo "<a href=\"delete_post.php?post_id=" . $row['post_id'] . "\">Delete</a>";

 

 

Here is my delete_post page.

 

 include "connect_to_mysql.php"; 


// SQL query
$sql = mysql_query ("DELETE FROM posts WHERE post_id = '$post_id'"); 

echo 'Post has been deleted! </br></br>Go to blog home!<a href="index.php">Click Here</a><br /></br>';

 

Just so you know post_id is the name of the id's in the database.. so I know my names are not wrong.

Link to comment
https://forums.phpfreaks.com/topic/266800-delete-statement-not-working/
Share on other sites

Hey thanks man, so would this work the same way if I wanted to delete the title of the post from the database instead of the entire row?

 

No, you would use an UPDATE for that, with an empty value. UPDATE posts SET post_title='' WHERE post_id=$post_id

 

 

With that aside, you are doing something very wrong.

<a href=\"delete_post.php?post_id=" . $row['post_id'] . "\">Delete</a> <-- DO NOT DO THIS!

 

GET should only be used for GETting data, not manipulating it in any way. Use a POST request to delete things, so that you can properly secure it from CSRF attacks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.