CMS for allowing users to delete or edit a post I have been working on a personal CMS for a site I've been running, and right now I'm working on a page that lists all entries, or posts from a database. I have given the user, with checkboxes and a drop down menu, the option to either delete or edit a post.

    //query database
    $query = mysql_query("SELECT * FROM userPosts");

    //turn query results into an array
    while($rows = mysql_fetch_array($query)) :

    //List all posts in a table (other table markup not shown).
    $posts = $rows['userTexts'];
    $id = $rows['ID'];
    echo "&lt;tr&gt;\n&lt;td&gt;" . $id . "&lt;/td&gt;\n&lt;td class='posts'&gt;" . $posts . "&lt;/td&gt;\n&lt;td&gt;&lt;input type='checkbox' name='chkBox' value='chk[". $id . "]'&gt;&lt;/td&gt;\n&lt;/tr&gt;\n";
endwhile


This part is not the problem, but I figure it is necessary for the question. Is there a way to make this code cleaner, without so many if statements?

$chkWhat = $_REQUEST["chkBox"];
$options = $_REQUEST["options"];


if(isset($_REQUEST["submit"])) {
    if($chkWhat != "chk[" .$id . "]") {
        echo "&lt;p&gt;Please select a post.&lt;/p&gt;";
    } else {
        if( //more than one check box is checked but I still have to figure this...) {
            echo "&lt;p&gt;For now, please just select 1 post you would like to edit.&lt;/p&gt;";
        } else {
            //edit your post
            echo "You will edit your post";
        }
        if($options == "delete") {
            //delete your post(s) here
            echo "You will delete your post(s)";
        }
    }
}


Sorry about the second if statement there, but I am still researching that.
Usually you can use switch statements to clean up abundant if statements, but only if those if statements are similar; Such as checking for different values of the same variable. Since that is not the case here, there isn't much you can change in this area. Sometimes you just need a few if statements. I would, however, suggest that you not use the global $_REQUEST as it is rather insecure. Even if you think you are the only one who is ever going to use it, what if someone got in, you just removed one more step of security from your site making it that much more easier for someone to destroy your hard work. Even you could make a mistake later by accidentally duplicating a POST variable as a GET variable, or vice-versa. Or someone with malicious intent does it on purpose, you could run into a lot of problems there. You should also sanitize those. Check out filter_input (>=PHP 5.2) to get started, or check other forums on the best way to sanitize your form variables.

Your delima with checking for multiple checkboxes should be rather easy. You've already got your form set up to make them into an array after post. Or almost right. You need to change name to what you have for your value. That should work, but if it doesn't try making the value the $id and just leaving the name chk[], should probably do that anyways as it won't leave empty or useless values. After that just check the count.

$checkboxes = $_POST['chk[]'];
if(count($checkboxes) &gt; 1) { etc... }


There is a minor change you should make to your if statements. Since you are checking $options for a "delete" value, you should also check it for an "edit" value, or move that if statement into the else block and give it an else for the edit block. Otherwise, as you can imagine, you'll end up editing and then deleting your posts and that would take up unneeded resources.

if( //more than one check box is checked but I still have to figure this...) {
    echo "&lt;p&gt;For now, please just select 1 post you would like to edit.&lt;/p&gt;";
} else {
    if($options == "delete") { /*delete your post*/ }
    else { /*edit your post*/ }
}


I don't see anything else really. So good luck!