Stu
Stu

Reputation: 1861

Trouble deleting a record using PHP. Code included

I Have simple PHP functions that produce html links which should delete a record, however the delete link only manages to refresh the page.

I know the solution is simple but I am new to PHP so could somebody please be kind enough to point me in the right direction? thank you. help greatly appreciated.

fuctions.php

<?php
     include('includes/connect.php'); 
  function getPosts() {
        $query = mysql_query("SELECT * FROM posts") or die(mysql_error());
        while($post = mysql_fetch_assoc($query)) {
            echo "<tr><td>" . $post['Title'] . "</td><td>" . $post['Author'] . "</td><td><a href=\"delete.php?id=" . $post['ID'] . "\">Delete</a><br /><a href=\"edit.php>?id=" . $post['ID'] . "\">Edit</a></td></tr>";
        }
    }
    function deletePost($id) {
        $id = (int) $id;
        mysql_query("DELETE FROM posts WHERE ID = '$id'") or die(mysql_error());
        header("Location: posts.php");
  }
?>

delete.php

<?php
   include('includes/functions.php');
   deletePost($_GET['ID']);
?>

Upvotes: 1

Views: 204

Answers (3)

Oleg
Oleg

Reputation: 2821

Print your sql delete command before executing and it I suppose will answer your question.

Upvotes: 0

Kristoffer Sall-Storgaard
Kristoffer Sall-Storgaard

Reputation: 10636

Check your casing.

You are setting the GET paramets name to id while checking for ID

Upvotes: 0

Michael Irigoyen
Michael Irigoyen

Reputation: 22947

In your delete.php file, you call:

deletePost($_GET['ID']);

However, in your link you use:

delete.php?id=

It is a case issue, make them both be upper case ID or lower case id.

Upvotes: 4

Related Questions