delete link does not give errors but wont delete post

Hi there! I´m new to php and I´m trying to create a blog from a brief tutorial. And it seems like I can´t get the posts to delete in the database. I´ve been trying to look for errors for the last couple of hours. I just get back to the blog.php file and nothing is removed. I removed the “delete-link” in post.php cause I´m not sure how to type it correctly anyway :-[ Can you help me? /Cat :slight_smile:

post.php (sorry if html-code aren´t supposed to go in here)
[php]<?php
include(“connect.php”);

mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“blog”);

$postid = (int)trim($_GET[‘post’]);
$sql = "SELECT posts.ID as ID,
users.postname as postname,
posts.title as title,
posts.content as content,
posts.date as date

FROM posts

INNER JOIN users

ON users.ID = posts.username

WHERE posts.ID = $postid
ORDER BY date DESC";
$result = mysql_query($sql);
$postdata = array();
while($row=mysql_fetch_array($result)){
$postdata = $row;
}

?>

<?php echo '<?xml version="1.0" encoding="UTF-8"?>';

?>

Blog title -> <?php echo htmlentities($postdata['title']); ?>
Blog title
<?php echo htmlentities($postdata['postname']); ?> Wrote <?php echo htmlentities($postdata['title']); ?> at <?php echo date('l jS \of F Y h:i:s A',(int)$postdata['date']); ?>
<?php echo $postdata['content']; ?>



Edit   
    </div>
</div>
[/php]

delete: [php]<?php

include(‘connect.php’);

$conn = mysql_connect(“localhost”,“root”,"");
if (!$conn)
{
die('Could not connect to the database: ’ . mysql_error());
}

mysql_select_db(“blog”, $conn);

if (isset($_GET[‘id’]) && is_numeric($_GET[‘id’]))
{

$id = $_GET[‘id’];

$result = mysql_query(“DELETE FROM posts WHERE id= ‘$id’”)
or die(mysql_error());

// redirect back to the view page
header(“Location: blog.php”);
}
else
// if id isn’t set, or isn’t valid, redirect back to view page
{
header(“Location: blog.php”);
}

?>[/php]

Your delete link should look like this:
[php]Delete[/php]
(you can also add some javascript confirmation popup via onClick event)

On Unix/Linux MySQL database field names are case sensitive. I see in your post.php you have field ‘ID’ in the table ‘posts’. But in the delete.php you spell it in lowercase ‘id’ - this need to be fixed:
[php]$result = mysql_query(“DELETE FROM posts WHERE ID= ‘$id’”)[/php]

And finally (not critical), you have this include statement in each file:
[php]include(“connect.php”);[/php]
It seems the code in the connect.php is supposed to connect to a database & select database. I suggest to move these two lines from post.php and delete.php to this connect.php (if these lines are already in place there, then just delete them from post.php and delete.php):
[php]
mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“blog”);
[/php]

Thank you so much for your kind walkthrough! :slight_smile: Now it works just fine!

Sponsor our Newsletter | Privacy Policy | Terms of Service