用一次执行更新三个表

时间:2012-11-13 06:47:37

标签: sql codeigniter sql-update

我尝试用一​​个按钮执行来更新三个表,但为什么只是改变了t_user? t_publisher和t_label没有改变,也没有错误警告..

function update($id_user=null){

        if (($this->input->post('submit') == 'Update')){
            $user=$this->input->post('username');
            $pass=$this->input->post('userpassword');
            $ussta=$this->input->post('userstatus');
            $usty=$this->input->post('usertype');
            $id_label = $this->db->query("select ID_LABEL from t_label where LABEL = '$name->USER_NAME'")->row();
            $id_publisher = $this->db->query("select ID_PUBLISHER from t_publisher where PUBLISHER = '$name->USER_NAME'")->row();

        $data = array(
            'USER_NAME' => $user,
            'USER_PASS' => $pass,
            'USER_STATUS' => $ussta,
            'USER_TYPE' => $usty
            );

        $data2 = array(
            'LABEL' => $user,
            );

        $data3 = array(
            'PUBLISHER' => $user,
            );

        $this->db->where('USER_ID', $this->input->post('id'));
        $this->db->update('t_user', $data);
        $this->db->where('ID_LABEL', $this->input->post('$id_label'));
        $this->db->update('t_label', $data2);
        $this->db->where('ID_PUBLISHER', $this->input->post('$id_publisher'));
        $this->db->update('t_publisher', $data3);
        redirect("registrasi/reg");
        }
        $var['data'] = $this->db->query("select * from t_user where USER_ID= '$id_user'")->row_array();  
        $this->load->view('update', $var);
        }

请帮助:)

1 个答案:

答案 0 :(得分:0)

尝试

function update( $id_user = NULL ){
    if ( ( $this->input->post( 'submit' ) === 'Update' ) ) {
        $user = $this->input->post( 'username' );
        $pass = $this->input->post( 'userpassword' );
        $ussta = $this->input->post( 'userstatus' );
        $usty = $this->input->post( 'usertype' );

        $sql = "SELECT ID_LABEL FROM t_label WHERE LABEL = ?";
        $id_label = $this->db->query( $sql, array( $name->USER_NAME ) )->row();

        $sql = "SELECT ID_PUBLISHER FROM t_publisher WHERE PUBLISHER = ?";
        $id_publisher = $this->db->query( $sql, array( $name->USER_NAME ) )->row();

        $data = array(
            'USER_NAME' => $user,
            'USER_PASS' => $pass,
            'USER_STATUS' => $ussta,
            'USER_TYPE' => $usty
            );

        $data2 = array(
            'LABEL' => $user,
            );

        $data3 = array(
            'PUBLISHER' => $user,
            );

        $this->db->where('USER_ID', $this->input->post('id'));
        $this->db->update('t_user', $data);

        $this->db->where('ID_LABEL', $id_label);
        $this->db->update('t_label', $data2);

        $this->db->where('ID_PUBLISHER', $id_publisher);
        $this->db->update('t_publisher', $data3);

        redirect("registrasi/reg");
    }

    $var['data'] = $this->db->query( "SELECT * FROM t_user WHERE USER_ID= '$id_user'")->row_array();
    $this->load->view('update', $var);
}

我有点困惑。它是模型文件吗?如果是这样,为什么会有redirect命令?还有$this->load->view()的原因?又一次,$ name来自哪里?您将$id_user指定为默认参数,在此文件中将其用于何处?