We help IT Professionals succeed at work.
Get Started

PHP MySQL row delets all in error

MrChrisDavids
on
168 Views
Last Modified: 2015-03-02
I've got a form with a incident issue and list of checkboxes related to users involved in the incident. The checkbox values goes into an array in the POST [userGroup].

I then need to insert all users into the involved table - works fine.

I then select all rows from the table with the incident ID and loop over them checking if they're in the array. If not - issue the SQL delete statement. The problem is that ALL rows with the incidentID ($iid) are deleted.The SQL code I'm echoing is correct though.. :(

    foreach ($_POST['userGroup'] as $val) {
        // Check if user's added already
        $sql = "SELECT userID FROM involved WHERE incidentID = $iid AND userID = $val";
        $checkSQL = mysql_query($sql) or die(mysql_error);
        $checkRows = mysql_num_rows($checkSQL); // if == 1, user is added
        if($checkRows == 0) {
            // Add user
            $sql = "INSERT INTO involved ( incidentID, userID ) VALUES ( $iid, $val )"; 
            $insertUser = mysql_query($sql);
        }
    }

    // Check if user is added but should not be
    $sql = "SELECT userID, involvedID FROM involved WHERE incidentID = $iid";
    $involvedSQL = mysql_query($sql) or die(mysql_error);
        while($row = mysql_fetch_assoc($involvedSQL)) {
            $involvedID = $row['involvedID'];
            echo "<br><br>Setting involvedID " . $involvedID . "<br>";
            $userID = $row['userID'];
            echo "Setting userID " . $userID . "<br>";
            
            if (!in_array($userID, $_POST['userGroup'])) {
                // Remove user
                echo "Could not find user " . $userID . " on involvedID " . $involvedID . "<br>";
                $delsql = "DELETE FROM involved WHERE involvedID = $involvedID";
                echo "<br>" . $delsql;
                $deleteSQL = mysql_query($delsql) or die(mysql_error);
            }
        }

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2020
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE