1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Checkbox Checked and Save Multiple Checkbox in database

Discussion in 'PHP' started by newphpcoder, Sep 9, 2015.

  1. #1
    Hi Sir,
    I creating a form with checkbox
    for example I checked the Add Employee and Upload Employee, after I click the Submit button
    the output is 2 and 3 which is correct but only the Upload Employee was checked.
    How can it be that the Add Employee and Upload Employee was checked.
    Also how can save checkbox check in the database.
    
    if(isset($_POST['submit']))
    {
     if(!empty($_POST['access'])) {
      $access = $_POST['access'];
      echo "You chose the following color(s): <br>";
      foreach ($access as $access_id){
      echo $access_id."<br />";
      }} // end brace for if(isset
    
      else {
    
      echo "You did not choose a color.";
    
      }
    }
    
    <input type="checkbox" name="access[]" id="access" value="1" <?php if($access_id==1) echo 'checked="checked"'; ?>>Search/View Employee Information<br>
    <input type="checkbox" name="access[]" id="access" value="2" <?php if($access_id==2) echo 'checked="checked"'; ?>>Add Employee<br>
    <input type="checkbox" name="access[]" id="access" value="3" <?php if($access_id==3) echo 'checked="checked"'; ?>>Upload Employee<br>
    <input type="checkbox" name="access[]" id="access" value="4" <?php if($access_id==4) echo 'checked="checked"'; ?>>Permission<br>
    <input type="checkbox" name="access[]" id="access" value="5" <?php if($access_id==5) echo 'checked="checked"'; ?>>Vacation<br>
    <input type="checkbox" name="access[]" id="access" value="6" <?php if($access_id==6) echo 'checked="checked"'; ?>>Calendar Event<br>
    
    <input type="submit" name="submit" id="submit" value="Submit">
    
    Code (markup):
    Thank you
     
    newphpcoder, Sep 9, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Oh,my... there's a lot wrong in that code.
    First off, you can't have the same IDs on all the checkboxes - for the code you're providing, there's no reason to have id's on them at all.
    Second, only one checkbox is checked because you're overwriting the value of $access_id each time you write to it.
    This is why we have loops...
    Better code:
    
    <?php
    
    $access = '';
    
    if (isset($_POST['submit'])) {
       if(!empty($_POST['access'])) {
         $access = $_POST['access'];
       } else {
         echo 'You didn\'t make any choices';
       }
    }
    
    $checkbox_elements = array(
                           1 => array(
                             'content' => 'Search/View Employee Information'
                           ),
                           2 => array(
                             'content' => 'Add Employee'
                           ),
                           3 => array(
                             'content' => 'Upload Employee'
                           ),
                           4 => array(
                             'content'   => 'Permission'
                           ),
                           5 => array(
                             'content' => 'Vacation'
                           ),
                           6 => array(
                             'content' => 'Calendar Event'
                           )
                         );
    
    echo '<form method="post" action="#">';
    foreach ($checkbox_elements as $key => $value) {
       echo '<input type="checkbox" name="access['.$key.']" id="access_'.$key.'" value="'.$key.'" '.((!empty($access[$key])) ? 'checked' : '').'><label>'.$value['content'].'</label><br>';
    }
    echo '<input type="submit" name="submit" id="submit" value="Submit">
         </form>';
    
    ?>
    
    PHP:
     
    PoPSiCLe, Sep 9, 2015 IP
  3. newphpcoder

    newphpcoder Greenhorn

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    I already resolve that problem using this code:

    
    
    if(!empty($_POST['access']))
        {
        $access = $_POST['access'];
            $sql_check = "SELECT employee_no, access_id from tbl_permission WHERE employee_no = '$employee_no'";
          $res_check = $conn->query($sql_check);
     
          $count_check = $res_check->num_rows;
      
    
      // first insert if no data was inserted before
    
      //for example I choose 1,2,3,4
    
    // all 1,2,34 was save to the database
            if($count_check == 0)
            {
              foreach ($access as $access_id)
              {
            $selected_options[] = $access_id;
    
              $insert = "INSERT INTO tbl_permission (employee_no, fullname, access_id, email, username)
              VALUES
              ('$employee_no', '$employee_name', '$access_id', '$email', '$username')";
              $res_insert = $conn->query($insert);
              }
            }
    
    //if the employee is already exist and he uncheck the 2 and add check 5
    
    //what is my logic and code so that number 2 was deleted on the database and 5 will be inserted and 1,3, and 4 will not be inserted because its already inserted before.
    
      else
      {
    
    
    
    
    
      }
    
    }
    
    <input type="checkbox" name="access[]" id="access" value="1" <?php if(in_array(1,$selected_options)) echo 'checked="checked"'; ?>>Search/View Employee Information<br>
    <input type="checkbox" name="access[]" id="access" value="2" <?php if(in_array(2,$selected_options)) echo 'checked="checked"'; ?>>Add Employee<br>
    <input type="checkbox" name="access[]" id="access" value="3" <?php if(in_array(3,$selected_options)) echo 'checked="checked"'; ?>>Upload Employee<br>
    <input type="checkbox" name="access[]" id="access" value="4" <?php if(in_array(4,$selected_options)) echo 'checked="checked"'; ?>>Permission<br>
    <input type="checkbox" name="access[]" id="access" value="5" <?php if(in_array(5,$selected_options)) echo 'checked="checked"'; ?>>Vacation<br>
    <input type="checkbox" name="access[]" id="access" value="6" <?php if(in_array(6,$selected_options)) echo 'checked="checked"'; ?>>Calendar Event<br>
    
    
    PHP:
     
    newphpcoder, Sep 10, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Yeah... That doesn't really solve anything. Those SQL statements are wide open for attack, since you're not using prepared statements, just putting the variables blindly into the query. Also, you're still using the same id for every checkbox. Do it again.
     
    PoPSiCLe, Sep 10, 2015 IP
    deathshadow likes this.