简体   繁体   中英

how to insert multiple checkbox values in single column in database using php

i'm working on permissions in php,my requirement is i have list of menus..based on user role i have to give access to them.so a single user have access to multiple menus..my idea is i will select check boxes and store it in single database columns.and if i check any check box the value should be 'y' in database column field.and if not check means value should be 'x' in database column field(Example:y,x,x,y) i have a table like this following.. i'm working on permissions in php,my requirement is i have list of menus..based on user role i have to give access to them.so a single user have access to multiple menus..my idea is i will select check boxes and store it in single database columns.and if i check any check box the value should be 'y' in database column field.and if not check means value should be 'x' in database column field(Example:y,x,x,y) i have a table like this following..

role id,role name,permissions,description...

my code:

<form class="form-horizontal tasi-form" method="POST"  name="register-form" id="register-form">


                        <div class="form-group">
                            <label class="col-sm-2 col-sm-2 control-label">Roles Title</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control round-input" name="roletitle" id="roletitle">

                            </div>

                        </div>
                           <div class="form-group">
                            <label class="col-sm-2 col-sm-2 control-label">Permission</label>
                            <div class="col-sm-10">
                               <input type="checkbox" name="sb[]" id="checkbox_php" value="EMPLOYEES"/><label for="checkbox_php">EMPLOYEES</label> 
                                <br/>
                                <input type="checkbox" name="sb[]" id="checkbox_asp" value="UNIT"/><label for="checkbox_asp">UNIT</label><br/>  
                                <input type="checkbox" name="sb[]" id="checkbox_asp" value="SERVICES"/><label for="checkbox_asp">SERVICES</label><br/>  
                                <input type="checkbox" name="sb[]" id="checkbox_asp" value="Appointment"/><label for="checkbox_asp">Appointment</label><br/>    

                            </div>

                        </div>

                        <div class="form-group">
                            <label class="col-lg-2 col-sm-2 control-label">Roles Description</label>
                            <div class="col-lg-10">
                     <textarea name="description" id="editor1" class="form-control"  cols="30" rows="10"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-2 col-sm-2 control-label"></label>
                            <div class="col-lg-10">
                <input type="hidden" value="roles" name="requesttype"/>
                       <button type="submit" class="btn btn-success">Save</button>
                       </div>
                       </div>
                    </form>
php code:
<?php
include("../config.php"); 
if(isset($_POST['requesttype'])&&$_POST['requesttype'] == 'roles'){
   $insertInfo=array();
    $insertInfo['ROLES_TITLE'] = $_REQUEST['roletitle'];
    $insertInfo['PERMISSIONS'] = $_REQUEST['permission'];
    $insertInfo['ROLES_DESCRIPTION'] = $_REQUEST['description'];
    $date=date('Y-m-d H:i:s');
    $insertInfo['CREATED_ON']=$date;
    $insertinfo['UPDATED_BY']='';
    $insertInfo['UPDATED_ON']=$date;
$res=$db->insert('ROLES', $insertInfo);
if($result)
{?>
//header("location:city");
<script>
window.location.href='rolesa';
</script>
<?php }
} ?>

Create the table and with column which has datatype as varchar. Give values to each check boxes and add the checked checkboxes values in that column

<input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
<input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>

Add comma separated values in database column check the complete answer here

PHP : insert multiple check boxes values into one MySQL cloumn

I will suggest a alternative way, If you can store real value in db.

Like this,

dbCheckbox = "";
if(isset($_POST['sb'])){
  $dbCheckbox = implode(',',$_POST['sb']);
}

When you retrieve data you can,

$dbCheckboxExplode = array();
if(!empty($checkDBValueOnThatCol)){
  $dbCheckboxExplode = explode(",", checkDBValueOnThatCol);
}

And You better to keep checkbox value on display page as array like this,

<?php
$checkboxAllValues = array('EMPLOYEES', 'UNIT');

foreach($checkboxAllValues  as $value){
?>
<input type="checkbox" name="sb[]" id="checkbox_asp" value="<?php echo $value; ?>"/><label for="checkbox_asp"><?php echo $value; ?></label><br/> 
<?php
}
?>

When db data retrieve page,

<?php
$checkboxAllValues = array('EMPLOYEES', 'UNIT');

foreach($checkboxAllValues  as $value){
    $checked = "";
  if(in_array($value, $dbCheckboxExplode)){
      $checked = ' checked="checked"';
  }
?>
<input <?php echo  $checked; ?> type="checkbox" name="sb[]" id="checkbox_asp" value="<?php echo $value; ?>"/><label for="checkbox_asp"><?php echo $value; ?></label><br/> 
<?php
}
?>

You can do this way:

<?php
    $allValues = array('EMPLOYEES', 'UNIT');
    $dbCheckbox = "";
    if(isset($_POST['sb'])){
       foreach($_POST['sb'] as $value){
           if(in_array($value, $allValues)){
             $dbCheckbox .=  'Y';
           }else{
             $dbCheckbox .=  'X';
           }
            $dbCheckbox .=  ',';
       }
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM