lundi 7 novembre 2016

Submitting multiple checkbox values in HTML form.

I have a html form which displays the contents of a mySQL table called banner, containg multiple banner elements, that appear like:

Im using PHP Codeigniter. I want users to be able to update the display checkbox and submit, storing the values 1 and 0 in the database for checked / not.

FormView

    <td><input type="checkbox" name="bdisplay[]" value="<?php $bannerId?>"
    <?php if ($displaycheck==1): ?> checked <?php endif;?>>
    <?php echo $banner->bdisplay; ?></td>

The function for form submission. It creates a new associative array, if checkbox ticked id=>1 or else id=>0.

    public function do_updatedisp(){      
    $results = array();
    foreach($_POST['bdisplay'] as $onedisplay) {
    if(!empty($onedisplay)) {
    array_push($results, $results[$onedisplay]=1);
    }
    else{array_push($results, $results[$onedisplay]=0);}

    $this->bannerM->form_update($results);

    }  } 

The banner model:

function form_update($results){
foreach($results as $result=>$value){
    $this->db->set('bdisplay', $value);
     $this->db->where('banner_id', $result);
     $this->db->insert('banner');
}   

This is the error I get: Cannot add or update a child row: a foreign key constraint fails (vigilantx.banner, CONSTRAINT FK__usarios FOREIGN KEY (user_id) REFERENCES usarios (user_id))

I know there is nothing wrong with the foreign key, there is error elsewhere but I have been stuck on this for far too long! Any help appreciated!




Aucun commentaire:

Enregistrer un commentaire