插入后显示查询结果

时间:2012-12-18 15:16:44

标签: php codeigniter

请将查询插入数据库后如何将数据显示到视图页面。

查看页面(member_view.php):

<form action="<?php member_controller/insert_info; ?>" method="post">
<input type="text" name="fname">  
<input type="text" name="lname">
<input type="submit" name="submit">
</form>  

<?php 
if($res){
    foreach($res as $data){
    echo $data['fname'];  
    echo $data['lname'];
    }
}
?>

控制器页面(member_controller.php):

<?php  
class Member_Controller extends CI_Controller {

public function insert_info(){  

$data = array('fname' => $this->input->post('fname'),  
'lname' => $this->input->post('lname'),           
);  

$this->load->model('member_model');  
$this->member_model->member_posting($data);  
$data['username'] = $session_data['username'];
$retrieved_info['res'] = $this->member_model->member_posting($data);
$this->load->view('member_view',$data, $retrieved_info)
}

}

模型页面(member_model):

<?php
class Member_Model extends CI_Model{  

public function member_posting($data=array()){

extract($data);

$query = "INSERT INTO member_table (fname, lname)values ('$fname', '$lname')";
$result = $this->db->conn_id->prepare($query);
$result->execute();

if($result){
$query = "SELECT * FROM member_table WHERE fname = '$fname' AND lname='$lname'";
$result = $this->db->conn_id->prepare($query);
$result->execute();   
return $res = $result->fetchAll(PDO::FETCH_ASSOC);
}

}

}  

以下是模式:

查看(提交表格)----&gt;控制器(获取vars并发送给模型)---&gt;
model(插入到db并执行select查询)---&gt; controller(从模型中获取select查询结果)---&gt; view(显示结果)

请帮助我,我刚接触codeigniter,希望我能向你们学习。

2 个答案:

答案 0 :(得分:1)

这是您需要遵循的方式.........

查看文件:member_view.php

<form action="<?php echo ROOT_FOLDER ?>/member_controller/insert_info" method="post">
  <input type="text" name="fname">  
  <input type="text" name="lname">
  <input type="submit" name="submit">
</form>

控制器文件:member_controller.php

<?php  
     class Member_Controller extends CI_Controller {

        public function insert_info()

         {  

           $data = array(
                      'fname' => $this->input->post('fname'),  
                      'lname' => $this->input->post('lname'),           
                   );  

          $this->load->model('member_model');  
          $this->member_model1->member_posting($data);
          $retrieved_info = array();
          $retrieved_info['res'] = $this->member_model2->member_posting($data);
          $this->load->view('member_view',$data, $retrieved_info)
       }

   }

模型文件:member_model1.php

      class Member_Model extends CI_Model{  

        public function member_posting_inserting($data){

          $this->insert_helper('member_table',$data); 

         }

        public function member_posting_retrieving($data){

           $query = "SELECT * FROM member_table WHERE fname = '$data['fname']' AND lname='$data['lname']'";
           $res=$this->db->query($query);

            if($res->num_rows()>0){
             return $res->result("array");
             }
           return array();
         }  
       } 

插入辅助功能...............

   public function insert_helper($table_name, $data_array){
    $this->db->insert($table_name,$data_array);
    return $this->db->insert_id();      
}

答案 1 :(得分:0)

$this->load->view('member_view',$data, $retrieved_info)

在你的情况下不会做的伎俩。在此处阅读有关视图的信息:http://ellislab.com/codeigniter/user-guide/general/views.html

第二个参数是数据数组。第三个参数可以是TRUE或FALSE,决定是否必须呈现视图或将其作为字符串返回。

这应该可以解决问题:

$this->load->view('member_view', $retrieved_info);

编辑:

注意到你的member_posting()没有返回任何内容。它应该返回$ res;