为什么SELECT查询不能先来EDIT查询

时间:2016-09-23 04:05:23

标签: php sql database codeigniter

这个网络应用程序是一个包含成员数据的表格,如果我点击编辑,它将链接到已经选择了成员数据的编辑页面,因此它应该是SELECT首先

但 为什么SELECT查询不能先写,然后再编写EDIT查询。 我尝试将SELECT部分​​代码移动到上面但是何时运行程序,它无法编辑(不工作) PS。如果我写得如下,它的工作正常。但我想知道原因。

public function editMember($id)
{
    //UPDATE 
    if($this->input->post("btn") != null)
    {
        $arr = array(   
                "name" => $this->input->post('member_name'),
                "email" => $this->input->post('email'),
                "tel" => $this->input->post('phone')
            );
            $this->db->where("id", $id);
            $this->db->update('member', $arr);
            redirect("member", "refresh");
            exit();
    }

    $sql = "SELECT * FROM member WHERE id = $id"; //SELECT
    $rs = $this->db->query($sql);

    if($rs->num_rows() == 0)  //check the exist of data
    {
        $result['fetch'] = array();
    }
    else
    {
        $data['fetch'] = $rs->row_array(); // just fetch one row
        return $data['fetch'];
    }
}

1 个答案:

答案 0 :(得分:0)

Try this code

public function editMember($id)
{
    $sql = "SELECT * FROM member WHERE id = $id"; //SELECT
    $rs = $this->db->query($sql);

    //UPDATE 
    if($this->input->post("btn") != null)
    {
        $arr = array(   
                "name" => $this->input->post('member_name'),
                "email" => $this->input->post('email'),
                "tel" => $this->input->post('phone')
            );
            $this->db->where("id", $id);
            $this->db->update('member', $arr);
            redirect("member", "refresh");
            exit();
    }

    if($rs->num_rows() > 0)  //check the exist of data
    {
        $data['fetch'] = $rs->row_array(); // just fetch one row
    }


    $this->load->view('your_editpage.php',$data);
}