2017-03-01 61 views
0

我有一个表格由多个复选框,输入字段,我想这些数据插入到表中的一列,这是我的形式:如何在笨插入一列多个字段的数据

<div id="container"> 
     <h1>property Detail</h1> 
    <form action="" method="post"> 
    <table> 
    <tr> 
    <td> 
    Possesion 
    <input type="checkbox" name="feature[]" value="possesion"> 
    </td> 
    <td> 
    Possesion1 
    <input type="checkbox" name="feature" value="possesion1"> 
    </td> 
    <td> 
    Possesion2 
    <input type="checkbox" name="feature" value="possesion2"> 
    </td> 
    <td> 
    Possesion3 
    <input type="checkbox" name="feature" value="possesion3"> 
    </td> 
    <td> 
    Possesion4 
    <input type="checkbox" name="feature" value="possesion4"> 
    </td> 
    </td> 
    </tr> 

    <tr> 
    <td> <input type="submit" name="submit" value="submit"></td> 
    </tr> 
    </table> 
    </form> 

    </div> 

这里是我的控制器:

function index(){ 
      $this->load->view('form'); 

      if($_POST){ 


      $data_feature = array (
      'feature' => $_POST['feature'] 
      ); 

      $data['var']= $this->Mdata->p_detail($data_feature); 
      } 

     } 

,这里是我的模型:

function p_detail($data_feature){ 
      $this->db->insert('feature',$data_feature); 
      return $this->db->insert_id(); 


} 

我只得到我的表中有一个特征值,我想获取用户选中的复选框的所有值。

问候

+1

首先改变'NAME =“功能”'到'名字=“功能[]”'所有复选框元素,然后通过很多关系价值控制器。并在此之后'implode()'函数将数组值转换为字符串并在数据库表字段中保存为逗号分隔值。希望能帮助到你!!! –

+0

如何将数组值转换为字符串 –

+0

在Google中输入精确的句子,并且还可以看到我的评论:) –

回答

0

保存它的最好方法是用json_encode来制作json字符串中的数据。有许多方法来保存数据是这样的:

首先纠正HTML使所有同名领域数组一样:

<input type="checkbox" name="feature[]" value="possesion"> 

方法1:

让您的数据作为JSON字符串保存:

$this->db->insert('feature',json_encode($data));//data is an array need to insert() 

方法2:

让您的数据序列化数组:

$this->db->insert('feature',serialize($data)); 

方法3:

让您的数据数组作为字符串:

$this->db->insert('feature',implode(",",$data)); 

如果你想按行插入到行,然后遍历循环发布数值如下图所示:

function index(){ 

      $this->load->view('form'); 

      if($_POST){ 

       $data_feature = $_POST['feature']; 

       $data = []; 
       foreach($data_feature as $f_key => $f_value){ 

        $data[$f_key]['var']= $this->Mdata->p_detail($f_value); 

       } 

      } 

     } 

而你的模态使用为;

function p_detail($data_feature){ 

      $this->db->insert('feature',$data_feature);//inserts into a single column 
      return $this->db->insert_id();//returns last inserted id 

} 
+0

我想插入它而不是逗号分隔的行? –

+0

所以你想要插入每个值在单独的行吗? –

+0

是的,我想要在单独的行中的每个值 –

0

鉴于变化所有name="feature"name="feature[]"

最好的办法是在列保存数值如下json string.Like。

控制器:

function index(){ 
      $this->load->view('form'); 

      if($_POST){ 
      $features = $_POST['feature'];//array of features 
      $json = json_encode($features);//json string you need to save it in database 
      $data['var']= $this->Mdata->p_detail($json); 
      } 

     } 

而且

型号

function p_detail($data_feature){ 
      $data = array('feature'=>$data_feature);//sets json string value to feature column 
      $this->db->insert('feature',$feature);//inserts into a single column 
      return $this->db->insert_id();//returns last inserted id 

} 

您需要使用json_decode()从在array获得这些值数据库。

+0

特征是用逗号分隔的一列插入 –

+0

这个字符串是json字符串保存在表列中的。 –

+0

伙计们,我也有一个严重的问题。我想从三个不同的'选择'中插入3个值到一个数据库列中。我尝试过这种方法,但没有成效。谁能帮我这个? –

0

如其他人建议,更改名称=“功能”来命名=“功能[]” 如果要插入的$ _ POST值

[“功能”],以多行在你的表,你可以循环插入

$feature_array = $_POST['feature']; 
$feature_len = count($feature_array); 
for ($i=0;$i<$feature_len;$i++) { 
    p_detail($feature_array[$i]); 
} 

function p_detail($data_feature) { 
    $this->db->insert('feature',$data_feature); 
    return $this->db->insert_id(); 
} 
0

您在萨加尔阿罗拉解决方案的评论是要插入行而不是逗号分隔。 为此,首先就得从名字=“功能”改变所有checkboxs的名字来命名=“功能[]”

然后在你的模型

$this->db->insert_batch('feature', $data_feature['feature']); 
0

你有一个很大的问题你应用程序设计,如果你继续这个想法 我想,你会有更新和删除方法的另一个问题。

我建议你创建一个功能表,可以管理所有的功能在你的后台程序,创建中间表property_feature。因为在这里你有很多之间功能财产

相关问题