未添加外键值

时间:2014-04-19 15:37:06

标签: codeigniter

大家好我是codeigniter的新手,我希望有些专家可以帮我解决我的问题。我现在的问题是无法传递外键值,这意味着当我有2个datatables.1是具有外国的用户键列名称为user_pro_id,而另一个表是主键为pro_id的配置文件表。现在我不得不将pro_id发送到user_pro_id。

他们总是提示我的错误是Undefined变量:来自model / user的pro_id。 消息:"列' user_pro_id'不能为空" 状态:"错误"

这是我的控制器

public function create_user_submit() {
  $this->load->database();
  $this->init();
  $this->db->trans_start();
  $this->load->model('user');
  $this->load->model('user_profile');
  $post = array(
    'name' => rawurldecode($_POST['name']),
    'ic' => rawurldecode($_POST['IC']),
    'email' => rawurldecode($_POST['email']),
    'tele' => rawurldecode($_POST['tel']),
    'title' => rawurldecode($_POST['title']),
    'username' => rawurldecode($_POST['username']),
    'password' => rawurldecode($_POST['password'])
  );
  $data = $this->user_profile->create_user_profile($post['name'],$post['ic'],$post['email'],$post['tele'],$post['title'],$this->login['user_name']);

  if ($data === false) {
    $this->json_error($this->user->message);
    return;
  }
  $data1 = $this->user->create_user($post['username'],$post['password'],$this->login['user_name']);

  if ($data1 === false) {
    $this->json_error($this->user->message);
    return;
  }
  $profile_id = $this->user_profile->create_user_profile($profile_name,$profile_ic,$profile_email,$profile_tel,$profile_title,$this->login['user_name']);

  if ($profile_id === false) {
    $this->message = $this->db->_error_message();
  }
  $create_user = $this->create_user($username, $password, $this->login['user_name'],$profile_id);

  if ($create_user === false) {
    $this->message = $this->db->_error_message();
  }
  $this->db->trans_complete();

  if ($this->db->trans_status() === false){
    return false;
  }
  $this->json_ok();
}

模型用户

public function create_user($username, $password, $user_create) {
  $this->load->database();
  $user = $this->get_user_name_row_case_insensitive($username);

  // $profile_id = $this->get_profile_id($user_pro_id);
  if (sizeof($user) > 0) {
    $this->message = $this->error('username_exist');
    return false;
  }
  $seeds = substr(uniqid(), 0, 8);
  $gpassword = base64_encode(md5($password."_".$seeds, true));
  $sql = "insert into user(user_name, user_password, user_seed,user_pro_id,user_created_by, user_created_date)
         values(?, ?, ?, ?,?, current_timestamp)";
  $query = $this->db->query($sql, array(
    $username,
    $gpassword,
    $seeds,
    $profile_id,
    $user_create
  ));

  if ($query === false) {
    $this->message = $this->db->_error_message();
    return false;
  }
  return true;
}

model user_profile

public function create_user_profile($profile_name,$profile_ic,$profile_email,$profile_tel,$profile_title,$profile_create) {
  $this->load->database();
  $sql = "insert into profile(pro_name, pro_ic, pro_email, pro_tel_mobile, pro_job_title, pro_created_by, pro_created_date)
          values(?,?,?,?,?,?, current_timestamp)";
  $query = $this->db->query($sql,array(
    $profile_name,
    $profile_ic,
    $profile_email,
    $profile_tel,
    $profile_title,
    $profile_create
  ));

  if($query === false) {
    $this->message = $this->db->_error_message();
    return false;
  }
  $pro_id = $this->db->insert_id();
  return $pro_id;
}

希望你们能在这些问题上帮助我。在这个问题上坚持了很长时间=(

0 个答案:

没有答案