1

我有一张要插入多个国家/地区的汇率表。

这是我的表格

<? form_open_multipart('exchange/create')
?>
<input  type="text" name="user[0][ExchangeRateDate]"/>
<input  type="text" name="user[0][CountryId]"/>
<input  type="text" name="user[0][CashSelling]"/>
<input  type="text" name="user[0][CashBuying]"/>
<input  type="text" name="user[0][TransferSelling]"/>
<input  type="text" name="user[0][TransferBuying]"/>
<input  type="text" name="user[0][InsertDate]"/>
<input  type="text" name="user[0][Status]"/>

<input  type="text" name="user[1][ExchangeRateDate]"/>
<input  type="text" name="user[1][CountryId]"/>
<input  type="text" name="user[1][CashSelling]"/>
<input  type="text" name="user[1][CashBuying]"/>
<input  type="text" name="user[1][TransferSelling]"/>
<input  type="text" name="user[1][TransferBuying]"/>
<input  type="text" name="user[1][InsertDate]"/>
<input  type="text" name="user[1][Status]"/>
<input type="submit" value="insert exchange" class="btn"/>
</form>

她是当前表格的模型代码

public function set_exchange() {
    $data = array('ExchangeRateDate' => $this -> input -> post('ExchangeRateDate'),
   'CountryId' => $this -> input -> post('CountryId'),
   'CashSelling' => $this -> input -> post('CashSelling'),
   'CashBuying' => $this -> input -> post('CashBuying'),
   'TransferSelling' => $this -> input -> post('TransferSelling'),
   'TransferBuying' => $this -> input -> post('TransferBuying'),
   'InsertDate' => $this -> input -> post('InsertDate'),
 'Status' => $this -> input -> post('Status'));
   return $this -> db -> insert('exchange_rate', $data);    
    }

最后这是控制器代码

public function create() {

        $this -> load -> helper('form');
        $this -> load -> library('form_validation');
        $data['title'] = 'Create a news exchange rate';
        $this -> form_validation -> set_rules('ExchangeRateDate', 'ExchangeRateDate', 'required');
        $this -> form_validation -> set_rules('CountryId', 'CountryId', 'required');
        $this -> form_validation -> set_rules('CashSelling', 'CashSelling', 'required');
        $this -> form_validation -> set_rules('CashBuying', 'CashBuying', 'required');
        $this -> form_validation -> set_rules('TransferSelling', 'TransferSelling', 'required');
        $this -> form_validation -> set_rules('TransferBuying', 'TransferBuying', 'required');
        $this -> form_validation -> set_rules('InsertDate', 'InsertDate', 'required');
        $this -> form_validation -> set_rules('Status', 'Status', 'required');
        $this -> all -> set_exchange();
        $this -> load -> view('admin/exchange/create');

    }

表结构

ExchangeRateId
ExchangeRateDate
CountryId
CashSelling
CashBuying
TransferSelling
TransferBuying
InsertDate
Status

这个想法是一切正常我插入一个新行数据进入我的数据库但在这里我想插入不止一行只有ID不重复但其他是,所以我使用的表格来自其他卡住的溢出但它不工作

分享你的知识

提前问好……

4

1 回答 1

1

你的模型应该是这样的

public function set_exchange($data) {
    $this->db->insert('exchange_rate', $data);
    if ($this->db->affected_rows() == '1') return TRUE;
    return FALSE;    //error ocures
}

你的控制器应该是这样的

if(isset($_POST['insert_exchange'])) {
    //triggers only if form is sent
    $user = $_POST['user'];
    //$forms_sent = count($user); //this line is useless
    foreach ($user as $id => $index) {
        //$data['id'] = $id; //note 1 depends on your table structure
        foreach ($index as $key => $value) {
            $data[$key] = $value;
        }
        $this -> all -> set_exchange($data);
        var_dump($data); //for debug only
        unset($data);
    }
}

输出

Array ( [id] => 0 [ExchangeRateDate] => TestingValue1.1 
                  [CountryId] => TestingValue1.2 
                  [CashSelling] => TestingValue1.3 
                  [CashBuying] => TestingValue1.4 
                  [TransferSelling] => TestingValue1.5 
                  [TransferBuying] => TestingValue1.6 
                  [InsertDate] => TestingValue1.7 
                  [Status] => TestingValue1.8 ) 

Array ( [id] => 1 [ExchangeRateDate] => TestingValue2.1 
                  [CountryId] => TestingValue2.2 
                  [CashSelling] => TestingValue2.3 
                  [CashBuying] => TestingValue2.4 
                  [TransferSelling] => TestingValue2.5 
                  [TransferBuying] => TestingValue2.6 
                  [InsertDate] => TestingValue2.7 
                  [Status] => TestingValue2.8 )

至于HTML
<?= form_open_multipart('exchange/create')?> EDIT -等号

<?php echo form_open_multipart('exchange/create');?>

<input type="submit" name="insert_exchange" value="insert exchange" class="btn"/>

注意1: 取决于您的表的结构,我假设它看起来与此类似, id | ExchangeRateDate | CountryId | CashSelling ...因此 ID 是插入时必须具备的 :)

旁注:
对于将来,我建议将所有模型命名如下customer_model.php,视图如下customer_view

于 2013-05-30T14:01:04.957 回答