0

我想从数据库中选择 CodeIgnigher PHP 应用程序中的一些数据,并希望进行一些更改并插入到另一个表中。我想在 IN 表中插入尽可能多的选定记录。目前我只将一行插入到 out 表中。

您能否指出,这里做错了什么。

我的桌子是。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
        }
    }
4

1 回答 1

0

好吧,我在你提供的代码中看不到 in_table/out_table 的位置。

插入很多行你想要的 insert_batch

文档中的示例

$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;
}
于 2013-03-17T23:33:36.620 回答