请帮助我在将查询插入数据库后如何将数据显示到视图页面。
查看页面(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);
}
}
}
这将是模式:
视图(提交表单)---->控制器(获取变量并发送到模型)--->
模型(插入数据库并执行选择查询)--->控制器(从模型中获取选择查询结果)--- >查看(显示结果)
请帮助我,我刚接触codeigniter,希望我能向你们学习。