0

I'm trying to delete table rows base on the uncheck checkbox of my input fields like this,

input checkbox

So these are input fields,

Level 1<input value="1" class="checkbox" type="checkbox" name="rubric_chkbox[]" />
Level 2<input value="2" class="checkbox" type="checkbox" name="rubric_chkbox[]" />
Level 3<input value="3" class="checkbox" type="checkbox" name="rubric_chkbox[]" />

I'm using the next jquery code to get the data,

var uncheked = [];

$("input[name='rubric_chkbox[]']:not(:checked)").each(function()
{
    uncheked.push($(this).val()); 
});

console.log(uncheked);

Console.log return the array value ["3"] which is the unchecked checkbox value.

And this is my foreach loop on PHP,

if (isset($_POST['uncheked']) || isset($_POST['user_id']))
{
    $rubric_uncheck_value = $_POST['uncheked'];    
    $IDuser = $_POST['user_id'];

    foreach($rubric_uncheck_value as $rubric_uncheck)
    {
        $sql_check = "SELECT raw_selected_rubric
                      FROM rubric_selected
                      INNER JOIN cmat_composition ON rubric_selected.ID_cmat = cmat_composition.ID_cmat
                      WHERE rubric_selected.ID_users = '$IDuser'
                      AND raw_selected_rubric = '$rubric_uncheck'
                      AND rubric_selected.Saved = '1'";

        $result_check = mysqli_query($conn,$sql_check);

        if (mysqli_num_rows($result_check) > 0)
        {                
            $sql_raw = "DELETE raw_selected_rubric
                        FROM rubric_selected
                        WHERE raw_selected_rubric = '$rubric_uncheck'
                        AND ID_users = ''$IDuser'";

            mysqli_query($conn, $sql_raw);

            $sql_to_validated = "DELETE selected_rubric
                                 FROM validated_score
                                 WHERE selected_rubric = '$rubric_uncheck'
                                 AND ID_users = ''$IDuser'";

            mysqli_query($conn, $sql_to_validated);
        }
    }
}

But this does nothing. What can be the problem? Help please. Thank you.

9
  • 1
    What are you expecting it to do? This post needs more details before it can be answered. Can you confirm that the unchecked and userid are correctly being passed to your server? Commented Oct 24, 2018 at 15:44
  • I'm expecting that the return value 3 would be deleted from my database Commented Oct 24, 2018 at 15:48
  • And yes sir the unchecked and userid are correctly being passed. Commented Oct 24, 2018 at 15:48
  • Hey I'm sorry I'm such and idiot. I've solved it. My problem is that in my delete query above, I should first select the table that I want to delete some data like this, "DELETE FROM rubric_selected WHERE raw_selected_rubric = '$rubric_uncheck' AND ID_cmat = '$ID_cmat' AND ID_users = '$IDuser'"; Commented Oct 24, 2018 at 15:58
  • 1
    Don't worry, I've had 1000s of those moments. Next time, chat with your rubber duck about it :-) en.wikipedia.org/wiki/Rubber_duck_debugging Commented Oct 24, 2018 at 16:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.