如何使用php在数据库中的单个列中插入多个复选框值

时间:2015-08-13 07:22:22

标签: php

我正在处理php中的权限,我的要求是我有菜单列表...基于用户角色我必须访问它们。所以一个用户可以访问多个菜单..我的想法是我会选中复选框并将其存储在单个数据库列中。如果我选​​中任何复选框,则数据库列字段中的值应为“y”。如果不是,则检查表示数据库列字段中的值应为“x”(示例:y, x,x,y)我有一个如下表: 我正在处理php中的权限,我的要求是我有菜单列表...基于用户角色我必须访问它们。所以单个用户可以访问多个菜单..我的想法是我会选择复选框并将其存储在单个数据库列中。如果我选​​中任何复选框,则数据库列字段中的值应为“y”。如果不是,则检查表示数据库列字段中的值应为“x”(示例:y,x,x) ,y)我有一张如下表:

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

2 个答案:

答案 0 :(得分:0)

创建表并使用数据类型为varchar的列。 为每个复选框指定值,并在该列中添加选中的复选框值

public void test() throws Exception
{
    String home_logo_url="158321";
    String enough_talk_promo="1057406";

    System.out.println("Check for home_logo_url");  
    driver.get(baseUrl);
    String SiteWindow = driver.getWindowHandle(); // get the current window handle
    driver.findElement(By.xpath("//*[@id='logo']/a")).click();
    for (String PromoWindow : driver.getWindowHandles()) 
    {
        driver.switchTo().window(PromoWindow); // switch focus of     WebDriver to the next found window handle (that's your newly opened window)
    }
        String script = "return rlSerial;";
        String value = (String) ((JavascriptExecutor)driver).executeScript(script);
        Assert.assertEquals(value,home_logo_url);
        driver.close();
        driver.switchTo().window(SiteWindow);
        System.out.println("Pass");

        System.out.println("Check for enough_talk_promo");
        driver.get(baseUrl + "/category/tournaments/");
        driver.findElement(By.xpath("//*[@id='content']/div/div[4]/aside/div/div/p[1]/a")).click();
        for (String PromoWindow : driver.getWindowHandles())
        {
            driver.switchTo().window(PromoWindow); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        }
        String sr_enough_talk_promo = (String) ((JavascriptExecutor)driver).executeScript(script);
        Assert.assertEquals(sr_enough_talk_promo,enough_talk_promo);
        driver.close();
        driver.switchTo().window(SiteWindow);
        System.out.println("Pass");


  } 

在数据库列中添加逗号分隔值 检查这里的完整答案

PHP : insert multiple check boxes values into one MySQL cloumn

答案 1 :(得分:0)

我会建议另一种方法,如果你可以在db中存储实际价值。

像这样,

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

当您检索数据时,

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

最好将显示页面上的复选框值保存为这样的数组,

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

当db data检索页面时,

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

你可以这样做:

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