在codeigniter中获取Id用户

时间:2016-05-15 07:42:37

标签: php database codeigniter web database-connection

我有两个数据库。 tabel_jawaban和tabel_user。

在tabel_user中有一个名为“id_user”的字段

我希望在注册时将“ id_user从tabel_user ”移动到“ id_user in tabel_jawaban ”。

但我失败了。谁有人可以帮忙?有我的代码......

model_user:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Model_user extends CI_Model {
		function __construct() { 
		parent::__construct();
		$this->tbl = "tabel_user";
		} 
		
		function cek_user($username="",$password="") {
		$query = $this->db->get_where($this->tbl,array('username' => $username, 'password' => $password));
		$query = $query->result_array();
		return $query;
		}
		
		function ambil_user($username)
		{
        $query = $this->db->get_where($this->tbl, array('username' => $username));
        $query = $query->result_array();
        if($query){
            return $query[0];
        }
		}
		
		function ambil_iduser($idUser)
		{
        $query = $this->db->get_where($this->tbl, array('id_user' => $idUser));
        $query = $query->result_array();
        if($query){
            return $query[0];
        }
		}
		
		function getAllUser() {
		$this->db->from('tabel_user');
		return $this->db->get();
		}
		
		function deleteUser($id)
		{
		$this->db->where('id_user', $id);
        $this->db->delete('tabel_user');	
		}

	}

?>

Jawaban_model:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jawaban_model extends Ci_Model {

  public function Ambil() {
    $data = $this->db->query('select * from tabel_jawaban, tabel_user where tabel_jawaban.id_user=tabel_user.id_user');
    return $data;
  }

  public function AmbilNilai2($iduser) {
    $data = $this->db->query("select * from tabel_jawaban, tabel_user where tabel_jawaban.id_user=tabel_user.id_user AND tabel_jawaban.id_user='$iduser'");
    return $data;
  }

  public function Simpan($tabel, $data){
    $res = $this->db->insert($tabel, $data);
    return $res;
  }

  public function UpdateTotalNilai($id_jawaban,$data)
  {
    $this->db->where('id_jawaban',$id_jawaban);
    $this->db->update('tabel_jawaban',$data);

  }
}

控制器:

function daftar(){
		$ambil_akun = $this->session->userdata('uname');
        $idUser = $this->user_model->AmbilIdUser($ambil_akun);

		$data = array(			
			'id_user' => $idUser,
			'tgl_tes' => date("Y-m-d"),
		);

		$this->jawaban_model->Simpan('tabel_jawaban', $data);
		$id=mysql_insert_id();		

		redirect('dashboard/soaluser/'.$id);
		}

1 个答案:

答案 0 :(得分:0)

您的代码中存在小错字。据我所知,您的User.Identity.GetUserId()方法是

user_model->

然而,在你的控制器中,你在这里需要不同的方法,

function ambil_iduser($idUser)
        {
        $query = $this->db->get_where($this->tbl, array('id_user' => $idUser));
        $query = $query->result_array();
        if($query){
            return $query[0];
        }

方法的名称不同 $idUser = $this->user_model->AmbilIdUser($ambil_akun); ambil_iduser

正确的控制器调用状态应为

AmbilIdUser