ds11111111111111111 2015-12-21 13:43
浏览 50
已采纳

在CodeIgniter中使用if语句进行两次插入查询

I'm trying to add information in two tables by checking the user type. If user will be a normal user then record must be entered in first table and if user type is doctor then record must be entered in both tables. I'm working in CodeIgniter 3.6

public function model_signup()
{
$nameVar                = $this->input->post("signup_name");
$emailVar           = $this->input->post("signup_email");
$phoneVar           = $this->input->post("signup_phone");
$passwordVar            = $this->input->post("signup_password");
$ifDoctorVar            = $this->input->post("signup_ifdoctor");
$pmdcVar                = $this->input->post("signup_pmdc");

$signu_query            = $this->db->query("SELECT * FROM `doc_users` WHERE `user_email`='".$emailVar."'");
if($signu_query->num_rows() >0){
return false;
}
else
{

   $this->db->query("INSERT INTO `doc_users`
   (`user_nicename`,
   `user_login`,
   `user_email`,
   `display_name`,
   `user_phone`,
   `user_pass`,
   `user_type`,
   `user_registered`)
   VALUES ('$nameVar',
   '$emailVar',
   '$emailVar',
   '$nameVar',
   '$phoneVar',
   '$passwordVar',
   '$ifDoctorVar',
   NOW())");

    $user_id = $this->db->insert_id();

   if($ifDoctorVar=='Doctor') {    // checking if user is doctor or not

   $this->db->query("INSERT INTO `doc_doc_details`
   (`ID`,
   `pmdc_id`,
   `email`,
   `phone)
   VALUES ('$user_id',
   '$pmdcVar',
   '$emailVar',
   '$phoneVar')");
  return true;
   }

  }
 }
  • 写回答

1条回答 默认 最新

  • douyuepi6485 2015-12-22 04:51
    关注
    public function model_signup()
    {
    $nameVar                = $this->input->post("signup_name");
    $emailVar           = $this->input->post("signup_email");
    $phoneVar           = $this->input->post("signup_phone");
    $passwordVar            = $this->input->post("signup_password");
    $ifDoctorVar            = $this->input->post("signup_ifdoctor");
    $pmdcVar                = $this->input->post("signup_pmdc");
    
    $signu_query            = $this->db->query("SELECT * FROM 
    `doc_users`     WHERE `user_email`='".$emailVar."'");
    if($signu_query->num_rows() >0){
    return false;
    }
    else
    {
    
    $this->db->query("INSERT INTO `doc_users`
    (`user_nicename`,
    `user_login`,
    `user_email`,
    `display_name`,
    `user_phone`,
    `user_pass`,
    `user_type`,
    `user_registered`)
    VALUES ('$nameVar',
    '$emailVar',
    '$emailVar',
    '$nameVar',
    '$phoneVar',
    '$passwordVar',
    '$ifDoctorVar',
     NOW())");
    
     $user_id = $this->db->insert_id();
    
     if($ifDoctorVar=='Doctor') {    // checking if user is doctor or not
    
     $this->db->query("INSERT INTO `doc_doc_details`
     (`ID`,
      `pmdc_id`,
      `email`,
      `phone)
      VALUES ('$user_id',
      '$pmdcVar',
      '$emailVar',
      '$phoneVar')");
        // return true remove this
      }
    
     }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?