无法在Codeigniter中插入数据库

时间:2016-05-13 06:57:31

标签: php database codeigniter

我正在尝试添加表格中的所有内容(交易),然后尝试将所有总计插入另一个表格(reports_daily),但没有插入任何内容。

transaction.php

 public function index()
 {
      $this->load->model('transactions_model');  
      $transresult = $this->transactions_model->get_transactions_list();           
      $data['translist'] = $transresult;
      $this->load->view('transactions_view',$data);
 }
 function create() {

     $transresult = $this->transactions_model->get_transactions_list();           
      $data['translist'] = $transresult;
        $sum=0;
        $items=0;
        $orders=0;
        $date=$translist[0]->date_d;
        for ($i=0; $i<count($translist); ++$i){
          $sum += $translist[$i]->total_amount;
          $items += $translist[$i]->no_items;
          $orders++;
        }
      $data = array(
        'date_d' => $date,
        'total_items' => $items,
        'total_orders' => $orders,
        'total_sales' => $sum
      );
      //$this->db->set($data);
      //$this->db->insert('reports_daily', $data);
      $this->transactions_model->insert_reports($data);
      $this->index();


  }

transactions_model.php

 function get_transactions_list()
 {
      $sql = 'select * from transactions order by date_d desc';
      $query = $this->db->query($sql);
      $result = $query->result();
      return $result;
 }
 function insert_reports($data) 
 {
      $this->db->insert('reports_daily', $data);
      return;
 }

帮助:(

1 个答案:

答案 0 :(得分:0)

控制器上不存在

$translist。因此,请使用$transresult[0]代替$translist[0];

function create() {

 $transresult = $this->transactions_model->get_transactions_list();           

    $sum=0;
    $items=0;
    $orders=0;
    $date = $transresult[0]->date_d;
    for ($i=0; $i<count($translist); ++$i){
      $sum += $translist[$i]->total_amount;
      $items += $translist[$i]->no_items;
      $orders++;
    }
  $data = array(
    'date_d' => $date,
    'total_items' => $items,
    'total_orders' => $orders,
    'total_sales' => $sum
  );
  //$this->db->set($data);
  //$this->db->insert('reports_daily', $data);
  $this->transactions_model->insert_reports($data);
  $this->index();

}