Link to home
Start Free TrialLog in
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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of austinfx
austinfx

ASKER

Sometimes it helps to get direct answers the the actual questions posted.
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!