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
      }
    
     }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站