Avatar of austinfx
austinfx
 asked on

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

PHPMySQL Server

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

This article may be helpful.  Generally speaking, it looks like your $_POST array may contain "sub-arrays" for the checkboxes.
https://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.
austinfx

ASKER
Sometimes it helps to get direct answers the the actual questions posted.
ASKER CERTIFIED SOLUTION
NerdsOfTech

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy