PHP delete function in a forum
<p>I have created this php file to allow users to delete posts on my forum. What i am trying to do is make sure users will be able to delete only their own posts UNLESS they are an administrator. So far users are able to delete their own posts. However Administrators cannot delete posts that they did not create and the code returns false at the DELETE query. What am i doing wrong?</p>

<p>user_level at 2 or higher is an administrator. 0 is for normal user.</p>

<p>Note: i am aware of the SQL injection vulnerabilities in this code. I will fix it once the logical part is complete.</p>

<pre><code>&lt;?php
include 'connect.php';
include 'header.php';

$id = $_GET['post_id'];

if (isset($_SESSION['signed_in'])) {

    $sql= "SELECT * FROM posts WHERE post_id=$id";
    $post_data=$link-&gt;query($sql);
    if($post_data-&gt;num_rows &gt; 0) {
        if($row=$post_data-&gt;fetch_assoc()) {
        }
    }
    if (!$_SESSION['user_level'] === 2  ) {

        echo 'You need to be an admin to delete other peoples posts.';
    }
    else {

        if ($_SESSION['user_id'] === $row['post_by']) {
            $delsql2= 'DELETE FROM posts WHERE post_id='.$id;
            $stmt2= $link-&gt;query($delsql2);
            header ('Location:index.php');
        }
        else {
            var_dump($_SESSION);
            echo 'You can only delete your own posts.';
        }
    }
}
else {
    echo 'You must be signed in to delete a post.';
}
?&gt;
</code></pre>