having trouble updating multiple records with implode

I am trying to update multiple records based on an array that is posted.

It works if the array has only 1 item but two or more and it fails to work.
if (isset($_POST['checkboxArray'])) {
    if (isset($_POST['archiveit'])){
        $checkbox = $_POST['checkboxArray'];
        $mID = implode( ",", $checkbox );
        $sql ="update simpleNote SET status='archived' WHERE  mID = '$mID'";

        if ($conn->query($sql) === TRUE) {
            echo "Items moved to archived.";
        } else {
            echo "Error archiving Messages: " . $conn->error . "<br/> SQL: " . $sql;
        }

        $conn->close();
    }
    if (isset($_POST['trashit'])){
        $checkbox = $_POST['checkboxArray'];
        $mID = implode( ",", $checkbox );
        $sql ="update simpleNote SET status='deleted' WHERE  mID = '$mID'";

        if ($conn->query($sql) === TRUE) {
            echo "Items deleted.";
        } else {
            echo "Error deleting Messages: " . $conn->error . "<br/> SQL: " . $sql;
        }

        $conn->close();
    }
}

Open in new window

austinfxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This article may be helpful.  Generally speaking, it looks like your $_POST array may contain "sub-arrays" for the checkboxes.
http://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

Please post the HTML document that is used to submit the form.  We probably need to use an iterator like foreach() to access the multiple checkbox data -- we will know that when we see the HTML form.
austinfxAuthor Commented:
Sometimes it helps to get direct answers the the actual questions posted.
NerdsOfTechTechnology ScientistCommented:
Your current query can match ONLY one value due to the equal sign (EXACT SEARCH) in the WHERE clause.

$sql ="update simpleNote SET status='archived' WHERE  mID = '$mID'";

Open in new window


Thus, you'll need an IN clause to specify searches for 1 or multiple cases and encapulate the variable in parentheses:

$sql ="update simpleNote SET status='archived' WHERE  mID IN ($mID)";

Open in new window


Make this change to lines 5 and 18

WARNING: I urge that you clean/scrub the input to prevent malicious attacks / injections as this is very easy for an attacker to exploit if unfiltered. Be cautious to remove % and _ from inputs too (IN clause wildcard characters), as mysql_real_escape won't (if that's your choice of cleaner on the PHP end).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
To anyone coming across this question in the future, please read the WARNING first.  This is the reason we asked to see the HTML and suggested the use of the foreach() iterator.  However, because the author did not provide the HTML we are only able to issue a warning, and are unable to show the code that would have been appropriate to sanitize the external input.  As written this code is incredibly dangerous and hackable.  It would take no more than a few lines of code and a few minutes to ruin the database.  Please take to heart the warning from NerdsOfTech and do not follow this model without understanding and mitigating the risks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.