CodeIgniter没有正确插入sql数据

时间:2012-01-24 17:41:42

标签: php mysql codeigniter

$idgen = uniqid(rand(), false);
$churchName = $this->input->post('church_name');
$streetAddress = $this->input->post('street_address');
$locationalCity = $this->input->post('locational_city');
$locationalState = $this->input->post('locational_state');
$locationalZip = $this->input->post('locational_zip');
$locationalCountry = $this->input->post('locational_country');
$taxNum = $this->input->post('tax_exemption_number');**

$this->db->query("INSERT INTO church_repo (church_name, street_address, locational_address, locational_zip, locational_country, locational_city, overseer_account_id, tax_exemption_number, status) VALUES('{$churchName}', '{$streetAddress}', '{$locationalCity}', '{$idgen}', '{$locationalState}', '{$locationalZip}', '{$locationalCountry}', '{$taxNum}', 'pending')");

上面的代码未正确插入,在Ci中我收到以下错误:

  

错误号码:1054

'字段中的未知列'locational_address'   list'

INSERT INTO church_repo(church_name,street_address,   locational_address,locational_zip,locational_country,   locational_city,overseer_account_id,tax_exemption_number,status)   VALUES('bgtg','ff','rgfr','270284f1eec6e5bfd4','rgrd','bdtbdt',   '美利坚合众国','84894894894','待定')

文件名:   C:\工作区\ htdocs中\一月-2012 \ Gospel-links.org \ SYSTEM \数据库\ DB_driver.php

线   数量:330

4 个答案:

答案 0 :(得分:3)

检查您的表属性名称,该错误表示您的表中不存在“locational_address”。可能只是一个错字

答案 1 :(得分:2)

错误是不言自明的:没有“locational_address”字段,正如d2byrke已经指出的那样,所以你应该先检查一下。

可能是“ street_address ”,也许?

作为附录,您没有逃避您在数据库中输入的值;如果您不想使用Active Record:

,请使用查询绑定
$churchName = $this->input->post('church_name');
$streetAddress = $this->input->post('street_address');
$locationalCity = $this->input->post('locational_city');
$locationalState = $this->input->post('locational_state');
$locationalZip = $this->input->post('locational_zip');
$locationalCountry = $this->input->post('locational_country');
$taxNum = $this->input->post('tax_exemption_number');

$sql = "INSERT INTO church_repo(church_name, street_address, locational_address, locational_zip, locational_country, locational_city, overseer_account_id, tax_exemption_number, status) VALUES(?,?,?,?,?,?,?,?,?)";

$this->db->query($sql, array($churchName,$streetAddress,$locationalCity,$locationalState,$locationalZip,$locationalChurch,$taxnum,'pending');

或者,甚至更干净(和受保护)的Active Record:

    $field['church_name'] = $this->input->post('church_name');
    $field['street_address'] = $this->input->post('street_address');
    $field['locational_city'] = $this->input->post('locational_city');
    $field['locational_state'] = $this->input->post('locational_state');
    $field['locational_zip'] = $this->input->post('locational_zip');
    $field['locational_country'] = $this->input->post('locational_country');
    $field['tax_exemption_num'] = $this->input->post('tax_exemption_number');
    $field['status'] = 'pending';
    $field['overseer_account_id'] = 'value here';

    $this->db->insert('church_repo', $field);

其中$field是一个数组,其中表名为索引,字段值为值。

答案 2 :(得分:0)

尝试此操作只需更改顺序或插入以匹配列

$idgen = uniqid(rand(), false);
$churchName = $this->input->post('church_name');
$streetAddress = $this->input->post('street_address');
$locationalCity = $this->input->post('locational_city');
$locationalState = $this->input->post('locational_state');
$locationalZip = $this->input->post('locational_zip');
$locationalCountry = $this->input->post('locational_country');
$taxNum = $this->input->post('tax_exemption_number');**

$this->db->query("INSERT INTO church_repo (church_name, street_address, locational_address, locational_zip, locational_country, locational_city, overseer_account_id, tax_exemption_number, status) VALUES('{$churchName}', '{$streetAddress}', '{$locationalCity}', '{$locationalZip}', '{$locationalState}', '{$locationalCountry}', '{$idgen}', '{$taxNum}', 'pending')");

答案 3 :(得分:0)

您需要清理/转义您要插入的内容。如果有'或其他东西你会遇到错误。确保您的数据库确实包含locational_address。复制/粘贴以确保没有拼写错误。

我会考虑改为这个,它更容易阅读和跟踪发生的事情。然后数据被正确转义。

$data = array(
   'church_name' => $this->input->post('church_name'),
   'street_address' => $this->input->post('street_address'),
   .....
   'tax_exemption_number' => $this->input->post('tax_exemption_number')
);

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