Hi

The user clicks on a buttons to delete all posts in a thread (this also deletes the parent post). The code is bellow. This however isnt working, its deleting all the parent posts, even those that dont correspond to the topic clicked on. Any ideas

if (isset ($_GET['delete']) && $_GET['delete'] == true) {
$id=$_GET['postid'];

$query = "DELETE FROM forum WHERE postid = '$id' OR parentid = '$id'";
if (mysql_query($query)) {
echo "<p width=700px>You have succesfully deleted the topic. Thankyou for keeping the forum clean. <br><a href='forum.php'>Back to forum</a><br><img src='images/tick.png'>";
include 'include/bottom.php';
die();
}
else 
$msg = "<p width=700px>You have tried to delete this message. However the following is stoping you: <br> <h3>Incorrect Priviledges</h3><br><a href='forum.php'>Back to forum</a><br><img src='images/cross.png'> "; 
include 'include/bottom.php';
die();
}

Dani AI

Generated

Following 's description and 's note about schema, the most likely causes are: the script is receiving an empty or non‑numeric id (which MySQL treats as 0 when compared to a numeric column), a mismatch between the link parameter and the name read by the script, or top‑level posts using 0 instead of NULL for parent pointers. When the id becomes '' or 0 the DELETE will match many rows and appear to remove unrelated parent posts.

Quick troubleshooting checklist:

  • Verify the actual GET payload (log or var_dump $_GET) to ensure postid is present and numeric.
  • Confirm whether top‑level rows use parentid = 0 or parentid IS NULL; adjust comparisons accordingly.
  • Preview which rows would be removed before running DELETE (use SELECT with the same WHERE).

Example safe workflow (input validation, preview, then delete inside a transaction using PDO):

if (!isset($_GET['postid']) || !ctype_digit($_GET['postid'])) { exit; }
$id = (int) $_GET['postid'];

$stmt = $pdo->prepare('SELECT postid, parentid, title FROM forum WHERE postid = ? OR parentid = ?');
$stmt->execute([$id, $id]);
$rows = $stmt->fetchAll(); // inspect this before deleting

$pdo->beginTransaction();
$pdo->prepare('DELETE FROM forum WHERE parentid = ?')->execute([$id]);
$pdo->prepare('DELETE FROM forum WHERE postid = ?')->execute([$id]);
$pdo->commit();

Longer‑term fixes: enforce referential integrity with InnoDB and a foreign key ON DELETE CASCADE (so child rows are removed automatically), or use a soft‑delete flag instead of hard deletes. Always run these changes on a copy and keep a backup before mass deletes.

How is your database set up? That will only work if
1. postid is unique to each post and parentid holds the id of the parent post and NULL if it is itself a parent post.
OR
2. parentid is set to the id of the topic for the parent post only (NULL otherwise) and postid holds the id that parentid holds but is set for child posts instead.

And of course, you pass the parent id to the script. Its hard to tell what's going wrong without knowing without knowing a bit more as the query seems ok.

(The query should only be "WHERE postid = '3' " if postid is a string, otherwise you should remove the single quotes)

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.