2017-08-16 4 views
0

問題は、保存ボタンをクリックすると、データがデータベースにリストされていることです。この場合、ユーザーの名前は頭の中で同じままです。私がlogged_inを実行した場合にのみ、ユーザーの名前は変更されます。ダッシュボードから外出することなく、セッションの助けを借りて名前を変更するにはどうすればいいですか?データ更新Codeigniter with session

モデル:

function get_user_by_id($id){ 
    $user = $this->ci->db 
     ->where($this->identifier_field, $id) 
     ->get($this->user_table); 

    $user_details = $user->row(); 
    return $user_details; 
} 

function get_user($data) 
{ 
    $this->ci->db->select('*'); 
    $this->ci->db->from('users'); 
    $this->ci->db->where('id', $data); 
    $query = $this->ci->db->get(); 
    $result = $query->result(); 
    return $result; 
} 

// Update Query For Selected Student 
    function update_user($id, $data) 
    { 
     $this->ci->db->where('id', $id); 
     $this->ci->db->update('users', $data); 
    } 

コントローラー:

この質問の私の理解から、
function show_user() 
{ 
    $data = array(
     'lang' => $lang, 
     'test_condition' => false, 
     'reg_condition' => false, 
     'current_page' => '', 
     'head_title' => $lang['profile'] 
    ); 

    $id = $this->session->userdata('identifier'); 
    $data['user'] = $this->authentication->get_user_by_id($id); 
    /*$data['single_user'] = $this->authentication->get_user($id);*/ 
    $this->load->view('profile', $data); 
} 

function profile() 
{ 
    $id = $this->input->post('id'); 
    if (isset ($id)) { 
     $data = array(
      'email' => $this->input->post('email'), 
      'birthday' => $this->input->post('birthday'), 
      'first_name' => $this->input->post('first_name'), 
      'last_name' => $this->input->post('last_name'), 
      'language_id' => $this->input->post('language'), 

     ); 
     $this->authentication->update_user($id, $data); 
    } 
    $this->show_user(); 
} 
+0

名前を印刷するために使用した変数を投稿するか、名前を印刷するヘッドビューブロックを投稿してください。 –

答えて

0

が、これはあなたの問題は今、私はそう感じる理由を説明させて頂いて

function profile(){ 

$id = $this->input->post('id'); 
if (isset ($id)) { 
    $data = array(
     'email' => $this->input->post('email'), 
     'birthday' => $this->input->post('birthday'), 
     'first_name' => $this->input->post('first_name'), 
     'last_name' => $this->input->post('last_name'), 
     'language_id' => $this->input->post('language'), 

    ); 
    $this->authentication->update_user($id, $data); 
} 
$this->show_user(); 
} 

に位置、

$ IDが設定されているかどうかをチェック次のプロファイル機能で、あなたのポストデータから$ IDを定義しますが、あなたのSHOW_USER方法を見て、あなたがセッションから

$id = $this->session->userdata('identifier'); 

を$ IDを定義し、まず、今か$ idが設定されていない、SHOW_USER機能が

$this->show_user(); 

今ではケースに$ idは、設定されていない場合、その後の更新処理が行われていない、まだあなたはまだSHOW_USER関数を呼び出す、という意味と呼ばれ、それがにありますデータベースの値は変更されませんが、show_functionを呼び出して更新された詳細を表示するとします。

解決策:まず、セッションから$ idを定義し、投稿からは定義しないでください。 ifブロック内にshow_user関数呼び出しを挿入します。更新が完了した場合にのみ呼び出されます。そうでない場合は別の操作を行います。プロファイル機能はこのようになります

function profile(){ 

    $id = $this->session->userdata('identifier'); 
    if ($id != null) { 
     $data = array(
      'email' => $this->input->post('email'), 
      'birthday' => $this->input->post('birthday'), 
      'first_name' => $this->input->post('first_name'), 
      'last_name' => $this->input->post('last_name'), 
      'language_id' => $this->input->post('language'), 

     ); 

     $this->authentication->update_user($id, $data); 

     redirect(base_url('classname/show_user')); 

    }else{ 
     //do something else 
    } 

} 

私はこれがあなたを助けてくれることを願っています。あなたが何か問題を見つけた場合は、親切にコメントし、それを並べ替えることができます

+0

非常にクールで、次の問題を提起しました。_エラー番号:1452追加または追加できません外字キー制約は失敗する( 'kyokushinkai_dev'.'users'、CONSTRAINT' users_ibfk_3')外部キー制約は失敗します(外部キー制約(' gender')参照 'genders'(' id'))_ – deadd1wer

+0

'function profile() { $ id = $ this-> session-> userdata( 'identifier'); if($ id!= NULL){$ データ=配列( のメール '=>の$ this - >入力 - >ポスト( 'メール')、 誕生日' =>の$ this - >入力 - >ポスト( '誕生日')、 'FIRST_NAME' =>の$ this - >入力 - >ポスト( 'first_nameの')、 'LAST_NAME' =>の$ this - >入力 - >ポスト( 'LAST_NAME')、 )。 ます$ this-> authentication-> update_user($ IDを、$データ); リダイレクト(base_url( 'show_user')); } else {$ this-> show_user(); }の$ this - > SHOW_USER(); } ' – deadd1wer

+0

しかし、それはあなたが概説問題ではありませんでした。私はあなたの要求に基づいて解決策を教えました –

0

保存ボタンをクリックすると。最後に挿入IDを取得し、このような検索クエリとそのレコードを見つけるために

$this->db->insert_id(); 

試し:

:このの助けを借りて、セッション

$query = $this->db->get_where('table_name',array('id'=>last inserted id)); 
$query = $query->row_array(); 

そして、その更新した後にこれを使用

$this->session->set_userdata('session name',$query); 
関連する問題