在Codeigniter中以表格形式发布多行数据

时间:2013-03-17 19:18:36

标签: php codeigniter

我想从数据库中选择CodeIgnigher PHP应用程序中的一些数据,并希望进行一些更改并插入到另一个表中。我想在IN表中插入与所选记录一样多的记录。目前我只在插件表中插入了一行。

请指点,这里做错了什么。

我的桌子是。 Out_Table(id,name ..)和In_Table(id,name,quantity ..)。

我的型号代码是:

<?php

    class ShoppingListM extends CI_Model {

    public function getData() {
        $query = $this->db->get('products');
        return $query->result();
    }

    function SaveForm($form_data) {
        $this->db->insert('SLists', $form_data);

        if ($this->db->affected_rows() == '1') {
            return TRUE;
        }

        return FALSE;
    }

}

?>

查看代码:             

        <div class="divTable">

        <fieldset>
            <!-- these will be affected by check all -->
     <div class="divRow">Product Name | Quantity | Packages</div>
            <div class="divRow"><input type="checkbox" size="100" class="checkall"> Check all</div>


            <br>

                <?php foreach ($records as $rec) {
                    ?>

                    <div class="divRow"><input type="checkbox">
                            <input size="5" type="hidden" value="<? echo $rec->id; ?>" name="id"></input>
                            <input size="20" type="text" value="<? echo $rec->name; ?>" name="name"></input>
                            <input size="5" type="text" value="" name="quantity"></input>
                            <select name="package">
                                <option name="case">Case</option>
                                <option name="box">Box</option>
                                <option name="box">Single Bottle</option>
                            </select>
                    </div>                
                    <br> 
                        <?
                    }
                    ?>


                    </fieldset> 
                    </div>
                    <div><input type="submit" name="submit"/></div>
                    </form>

控制器代码:

class ShoppingListController extends CI_Controller {

public function index() {
    //$data['val'] = array("test1", "test2", "test3");

   // $this->load->model('HomeModel');
    //$data['records'] = $this->HomeModel->getData();

    $this->load->model('ShoppingListM');

    $data['records'] = $this->ShoppingListM->getData();






    $this->form_validation->set_rules('name', 'Name', 'required|max_length[255]');          
    $this->form_validation->set_rules('qty', 'qty', '');                    
    $this->form_validation->set_rules('package', 'Package', 'required|max_length[128]');



            if ($this->form_validation->run() == FALSE) // validation hasn't been passed
    {
        $this->load->view('ShoppingListV', $data);
    }
    else // passed validation proceed to post success logic
    {
        // build array for the model

        $form_data = array(
                        'name' => set_value('name'),
                        'quantity' => set_value('quantity'),
                        'package' => set_value('package')
                    );

        // run insert model to write data to db

                    // run insert model to write data to db

        if ($this->ShoppingListM->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
        {
            redirect('ShoppingListController/success');   // or whatever logic needs to occur
        }
        else
        {
        echo 'An error occurred saving your information. Please try again later';
        // Or whatever error handling is necessary
        }
    }

1 个答案:

答案 0 :(得分:0)

我无法看到你提供的代码中的in_table / out_table在哪里。

插入许多你想要的行insert_batch

示例来自doc

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data); 

或者你可以从in_table中循环所有你的行,如果它是一个你不关心多个查询的维护工作的话,一个接一个地插入它们

$data= $this->db->get('Out_table');
try
{
    if ( $data->num_rows == 0)
         throw new Exception('table empty');

    foreach( $data->result() as $row)
    {

        //modifiy the row as u want
        $modified=$row;

        $q=$this->db->insert('in_table',$modified);
        if( ! $q )
            throw new Exception('failed to insert row id:'.$row->id);
    }
    echo $data->num_rows.'rows inserted';    
 }


catch (Exception $e)
{
    echo $e->getMessage();
 // Stop method execution with return, or use exit
    return;
}