如何在codeingiter中的多个表中插入记录

时间:2017-02-27 07:27:27

标签: php mysql codeigniter

我有以下表格。表单基本上包含两部分,一部分用于基本细节,第二部分用于功能,以下是代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title> Form </title>
</head>
<body>

  <div id="container">
    <h1>property Detail</h1>
    <form action="" method="post">
      <table>
        <tr>
          <td> City </td>
          <td> <input type="text" name="city"> </td>

          <td> Location </td>
          <td> <input type="text" name="location"> </td>

          <td> Marlas </td>
          <td> <input type="text" name="marlas"> </td>

        </tr>

        <tr>
          <td> Possesion
            <input type="radio" name="feature" value="possesion">
          </td>
          <td> Possesion1
            <input type="radio" name="feature" value="possesion1">
          </td>
          <td> Possesion2
            <input type="radio" name="feature" value="possesion2">
          </td>
          <td> Possesion3
            <input type="radio" name="feature" value="possesion3">
          </td>
          <td> Possesion4
            <input type="radio" name="feature" value="possesion4">
          </td>
        </tr>

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

我的数据库中有两个表,一个是具有此列(id,city,location,marlas)的详细信息,第二个是具有列(id,property_id,feature)的功能。我希望我的基本细节进入详细信息表,功能在功能表中。 到目前为止,我的基本细节将进入详细信息表,但我如何将功能插入到功能表中。

这是我的控制器

function property()
{
  if($_POST)
  {
    $data= array ( 'city' => $_POST['city'], 'location' => $_POST['location'],
      'marlas' => $_POST['marlas'] );
    $data['var']= $this->Mdata->p_detail($data);
  }
}

这是我的模型

function p_detail($data){
  $this->db->insert('detail',$data);
  return $this->db->insert_id();
}

2 个答案:

答案 0 :(得分:0)

只需在模型中创建另一个函数并在控制器中调用它。

控制器:

function property(){

    if($_POST){
    $data_detail = array (
       'city' => $_POST['city'],
       'location' => $_POST['location'],
       'marlas' => $_POST['marlas']
    );

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

    $data['detail_id']= $this->Mdata->p_detail($data_detail );
    $data['feature_id']= $this->Mdata->p_feature($data_feature, $data['detail_id'] );
    }   

} 

型号:

function p_detail($data){
    $this->db->insert('detail',$data);
    return $this->db->insert_id();
}

function p_feature($data, $detail_id){
    $data['property_id'] = $detail_id;
    $this->db->insert('feature',$data);
    return $this->db->insert_id();
}

答案 1 :(得分:0)

<强>形式:

<tr>
  <td> Possesion
    <input type="radio" name="feature[]" value="possesion">
  </td>
  <td> Possesion1
    <input type="radio" name="feature[]" value="possesion1">
  </td>
  <td> Possesion2
    <input type="radio" name="feature[]" value="possesion2">
  </td>
  <td> Possesion3
    <input type="radio" name="feature[]" value="possesion3">
  </td>
  <td> Possesion4
    <input type="radio" name="feature[]" value="possesion4">
  </td>
</tr>

<强>控制器:

function property()
{
  if($_POST)
  {
    $data= array ( 'city' => $_POST['city'], 'location' => $_POST['location'], 'marlas' => $_POST['marlas'] );
    $feature_data = array ( FEATURE_DATA );
    $data['var']= $this->Mdata->p_detail($data,$feature_data);
  }
}

<强>型号:

function p_detail($data,$featuredata){
  $data1 = $this->db->insert('detail',$data);
  $inert_one = $this->db->insert_id();
  $featuredata['property_id'] = $inert_one;
  $feature_data = $this->db->insert('feature_tbl',$featuredata);
  return $insert_one;
}