如何在表格的特定列中插入?

时间:2015-04-30 07:08:18

标签: sql codeigniter

我想在表格的特定列中插入内容: -

$this->db->insert('images.image',$data); // this query don't work

images中的我的表名,我的列名是image。我想在其中插入$ data。

我正在使用codeigniter。

2 个答案:

答案 0 :(得分:2)

插入数据时最好使用数据阵列。

免责声明:这只是示例代码帮助您。

模型功能示例

public function add($data) {
  $data = array(
    'CustomerName' => $data['CustomerName'],
    'ContactName' => $data['ContactName'],
    'Address' => $data['Address'],
    'City' => $data['City'],
    'PostalCode' => $data['PostalCode'],
    'Country' => $data['Country']
  );

  $this->db->insert('tablename', $data);
}

控制器功能示例

<?php

public function index() {
   $this->load->model('modal_name');

  // Your post data can go here also.
  // Example Only:

  $CustomerName = $this->input->post('CustomerName');

  if (isset($name)) {
    $data['CustomerName'] = $CustomerName;
  } else {
    $data['CustomerName'] = '';
  }

  // On view would be <input type="text" name="CustomerName" />

  $ContactName = $this->input->post('ContactName');

  if (isset($ContactName)) {
    $data['ContactName'] = $ContactName;
  } else {
    $data['ContactName'] = '';
  }

  // On view would be <input type="text" name="ContactName" />

  $Address = $this->input->post('Address');

  if (isset($Address)) {
    $data['Address'] = $Address;
  } else {
    $data['Address'] = '';
  }

  // On view would be <input type="text" name="Address" />

  $City = $this->input->post('City');

  if (isset($City)) {
    $data['City'] = $City;
  } else {
    $data['City'] = '';
  }

  // On view would be <input type="text" name="City" />

  $PostalCode = $this->input->post('PostalCode');

  if (isset($PostalCode)) {
    $data['PostalCode'] = $PostalCode;
  } else {
    $data['PostalCode'] = '';
  }

  // On view would be <input type="text" name="PostalCode" />

  $Country = $this->input->post('Country');

  if (isset($Country)) {
    $data['Country'] = $Country;
  } else {
    $data['Country'] = '';
  }

  // On view would be <input type="text" name="Country" />

  $this->load->library('form_validation');

  $this->form_validation->set_rules('CustomerName', 'Customer Name', 'required');

  if ($this->form_validation->run() == FALSE ) {
    $this->load->view('page', $data);
  } else {
    $this->model_name->add($this->input->post()); 
    // You can use $this->input->post() this will let you get all post with in this function

    /*
      var_dump($this->input->post());
      exit;
    */
    redirect('success_page');
  }
}

答案 1 :(得分:1)

您的控制器代码

$data=array('image'=>your_value);

$result = $this->your_model->insert($data);

和您的型号代码

function insert($data){
 $result = $this->db->insert('images',$data);
 }