【问题标题】:why SELECT query cannot come first and then EDIT query为什么 SELECT 查询不能先来,然后是 EDIT 查询
【发布时间】:2016-09-23 04:05:23
【问题描述】:

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

但是 为什么 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'];
    }
}

【问题讨论】:

    标签: php sql database codeigniter


    【解决方案1】:
    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多