lundi 8 juin 2020

check if all checkbox value exist in table

I am trying to check a table which values are stored by ids, if either of the selected ids exist i wish to show message the value already exist but with my current code say

id 2, 5, 6 exist in the table in checkbox i select 2, 6, 8 then its not working or 2, 6 then too it does not work but if i select just one value and if that exist only then the message shows but i wanted it to check all.

my code is

for($i = 0; $i<= 3; $i++){
    if(isset($_POST['exp'] [$i]))
        echo $can_exp .= ' '.$_POST['exp'] [$i];
}
$stmt = $db->prepare('SELECT form_id, exp_id FROM candidate_experience WHERE form_id = :form_id AND exp_id = :exp_id');
$stmt->execute(array(':form_id' => $rw['candidate_id'],':exp_id' => $can_exp));
$r = $stmt->fetch(PDO::FETCH_ASSOC);

if(!empty($r)){
    $error[] = 'Existing experience selected, kindly recheck selection.';
}

here is the checkbox

<div class="row form-group">
                            <div class="col-md-12">
                                <label for="activities">You have experience in?</label>                             
                                <div class="boxcheck">
                            <?php                   
                                        $stmt = $db->prepare('select * from experience order by (exp_name = "I have other job experience"), exp_name asc');
                                        $stmt->execute();
                                        $stmt = $stmt->fetchAll();
                                        foreach($stmt as $exp){
                                        ?>
                                        <input type="checkbox" name="exp[]" value="<?php echo $exp['expID']; ?>" <?php if(isset($_POST['exp']) && is_array($_POST['exp']) && in_array($exp['expID'], $_POST['exp'])) echo 'checked="checked"'; ?>> <?php echo $exp['exp_name']; ?><br/>                                         
                                        <?php
                                        }
                                        ?>
                    </div>

what is it that it only checks 1 instead of checking all?

please help me out, thanks




Aucun commentaire:

Enregistrer un commentaire