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

kumar

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 }
} ?>
user3099298

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 .=  ',';
       }
    }
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

inserting multiple checkbox values into a single column - SQL database

From Dev

How to insert checkbox values into table using PHP?

From Dev

How to insert checkbox values into table using PHP?

From Dev

get and display multiple checkbox values from database using php

From Dev

get and display multiple checkbox values from database using php

From Dev

How to insert multiple data to database using php?

From Dev

How to insert multiple jquery on created rows values into database using php and mysql?

From Dev

how to use checkbox values to access database using php?

From Dev

Insert multiple values in one column of mysql database

From Dev

How to get multiple values in single column when using group by?

From Dev

How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

From Dev

How the send multiple checkbox values to database in laravel?

From Dev

How to insert multiple inputs into the database using the power of PHP?

From Dev

how to insert multiple lines in json array in database using php

From Dev

how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

From Dev

how to insert multiple of values in a single field in mysql

From Dev

How to insert multiple values in a single query

From Dev

How to insert multiple values to single key in javascript

From Dev

PHP multiple checkbox to database

From Dev

PHP multiple checkbox to database

From Dev

How to extract comma separated column values from database using php

From Dev

insert multiple values in database

From Java

How to extract multiple parts of values of a single column?

From Dev

How to replace multiple values in a single column with SQL?

From Dev

How to insert a dynamic form values into database in php

From Dev

How do I insert multiple checkbox values into a table?

From Dev

How to insert array value into single column and single row in php

From Dev

SELECT multiple values from a single column in Wordpress Database

From Dev

How can i retrieve a single column from a database using a different column defined by the user? CI/PHP

Related Related

  1. 1

    inserting multiple checkbox values into a single column - SQL database

  2. 2

    How to insert checkbox values into table using PHP?

  3. 3

    How to insert checkbox values into table using PHP?

  4. 4

    get and display multiple checkbox values from database using php

  5. 5

    get and display multiple checkbox values from database using php

  6. 6

    How to insert multiple data to database using php?

  7. 7

    How to insert multiple jquery on created rows values into database using php and mysql?

  8. 8

    how to use checkbox values to access database using php?

  9. 9

    Insert multiple values in one column of mysql database

  10. 10

    How to get multiple values in single column when using group by?

  11. 11

    How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

  12. 12

    How the send multiple checkbox values to database in laravel?

  13. 13

    How to insert multiple inputs into the database using the power of PHP?

  14. 14

    how to insert multiple lines in json array in database using php

  15. 15

    how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

  16. 16

    how to insert multiple of values in a single field in mysql

  17. 17

    How to insert multiple values in a single query

  18. 18

    How to insert multiple values to single key in javascript

  19. 19

    PHP multiple checkbox to database

  20. 20

    PHP multiple checkbox to database

  21. 21

    How to extract comma separated column values from database using php

  22. 22

    insert multiple values in database

  23. 23

    How to extract multiple parts of values of a single column?

  24. 24

    How to replace multiple values in a single column with SQL?

  25. 25

    How to insert a dynamic form values into database in php

  26. 26

    How do I insert multiple checkbox values into a table?

  27. 27

    How to insert array value into single column and single row in php

  28. 28

    SELECT multiple values from a single column in Wordpress Database

  29. 29

    How can i retrieve a single column from a database using a different column defined by the user? CI/PHP

HotTag

Archive