dougou8552 2017-05-17 10:48
浏览 21

连接到另一个数据库并更新codeigniter中的行

I am trying to update the already existing username field where id=1 , from admin@something.com to the string in $username but the default database is being updated , i want to connect to the newly created db and update the username field where id=1 in pr_users table but unable to connect to the newly created db and update the field.

I am calling a model to update the table i.e using $data['applicant_rows'] = $this->mod_common->add_new_user($email,$userid,$username); somehow the statements after foreach($sqls as $statement) is not working, Please help

below is my controller code:

Controller: login.php

 public function save_userinput()
    {
      //code goes here
      // for example: getting the post values of the form:
      $form_data = $this->input->post();
      // or just the username:
      $username = $this->input->post("username");
       $email = $this->input->post("email");
       $userid = 1;
    $data['applicant_rows'] = $this->mod_common->update_table_with_user_for_the_first_time($email,$userid,$username);


//$data['workspace']    = $this->mod_common->update_admin($email);
     // $this->admin_list->manage_package();
      $this->load->dbforge();


  if ($this->dbforge->create_database($username))
{
    try{
      $current_database = $username;
      $this->db->database = $current_database;
    $data['applicant_rows'] = $this->mod_common->add_new_user($email,$userid,$username);
     $this->db->close();
     $config['hostname'] = "localhost";
     $config['username'] = "root";
     $config['password'] = "";
     $config['database'] = $current_database;
     $config['dbdriver'] = "mysql";
     $config['dbprefix'] = $username;
     $config['pconnect'] = FALSE;
     $config['db_debug'] = TRUE;
     $config['cache_on'] = FALSE;
     $config['cachedir'] = "";
     $config['char_set'] = "utf8";
     $config['dbcollat'] = "utf8_general_ci";
     $this->load->database($config);
    $fields = array(
                        'id' => array(
                                                 'type' => 'INT',
                                                 'constraint' => 11,
                                                 'unsigned' => TRUE,
                                                 'auto_increment' => TRUE
                                          ),
                        'home_bg' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '200',
                                          ),
                        'login_bg' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '200',
                                          ),
                        'other_bg' => array(
                                                 'type' =>'VARCHAR',
                                                 'constraint' => '200',

                                          ),
                        'uploaded_on' => array(
                                                 'type' => 'DATE',

                                          ),
                );

    $this->dbforge->add_field($fields);
    $this->dbforge->add_key('id', TRUE);
    $this->dbforge->create_table('pr_backgrounds', TRUE);


 $fields = array(
                        'id' => array(
                                                 'type' => 'BIGINT',
                                                 'constraint' => 15,
                                                 'unsigned' => TRUE,
                                                 'auto_increment' => TRUE
                                          ),
                        'username' => array(
                                             'type' => 'VARCHAR',
                                                 'constraint' => '255',
                                                 'default' => 'admin@ju.com',
                                          ),
                        'userpass' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '255',
                                                 'default' => '65e1b655a6d6f4cbed20554d3b52521a743afdc0', 
                                          ),
                        'email' => array(
                                                 'type' =>'VARCHAR',
                                                 'constraint' => '255',
                                                 'default' => 'admin@team.com', 
                                          ),
                        'departmentid' => array(
                                                  'type' => 'BIGINT',
                                                 'constraint' => 15,
                                               'default' => '1', 

                                          ),
                        'userroleid' => array(
                                                  'type' => 'BIGINT',
                                                 'constraint' => 15,
                                               'default' => '4', 

                                          ),
                        'managerid' => array(
                                                 'type' =>'VARCHAR',
                                                 'constraint' => '255',
                                                  'default' => '10', 
                                          ),
                        'userlevel' => array(
                                                  'type' => 'INT',
                                                 'constraint' => 11,


                                          ),
                        'branchid' => array(
                                                  'type' => 'INT',
                                                 'constraint' => 11,
                                               'default' => '2',
                                          ),
                        'is_global' => array(
                                                  'type' => 'TINYINT',
                                                 'constraint' => 4,

                                                 'default' => '0',
                                          ),
                        'registrationtime' => array(
                                                  'type' => 'INT',
                                                 'constraint' => 10,


                                          ),
                        'timemodified' => array(
                                                'type' => 'BIGINT',
                                                'constraint' => 10,


                                          ),
                        'modifierid' => array(
                                                  'type' => 'BIGINT',
                                                  'constraint' => 15,


                                          ),
                                                  'status' => array(
                                                  'type' => 'TINYINT',
                                                  'constraint' => 1,


                                          ),

                  'deleted' => array(
                                                  'type' => 'TINYINT',
                                                  'constraint' => 1,


                                          ),
                      'temppass' => array(
                                                  'type' => 'VARCHAR',
                                                  'constraint' => 20,


                                          ),

                         'temppassvalidtill' => array(
                                                  'type' => 'BIGINT',
                                                 'constraint' => 15,


                                          ),
                            'lastlogin' => array(
                                                  'type' => 'BIGINT',
                                                 'constraint' => 10,


                                          ),
                           'lastrefresh' => array(
                                                  'type' => 'BIGINT',
                                                 'constraint' => 15,


                                          ),
                                   'lastloginip' => array(
                                                  'type' => 'VARCHAR',
                                                 'constraint' => 20,


                                          ),
                                   'if_online' => array(
                                                  'type' => 'INT',
                                                 'constraint' => 11,


                                          ),
                                   'pfield' => array(
                                                  'type' => 'VARCHAR',
                                                 'constraint' => 255,


                                          ),
                );

    $this->dbforge->add_field($fields);
    $this->dbforge->add_key('id', TRUE);
    $this->dbforge->create_table('pr_users', TRUE);

$sql = file_get_contents("assets/zipbizzlatestdb.sql");


/*
Assuming you have an SQL file (or string) you want to run as part of the migration, which has a number of statements...
CI migration only allows you to run one statement at a time. If the SQL is generated, it's annoying to split it up and create separate statements.
This small script splits the statements allowing you to run them all in one go.*/


$sqls = explode(';', $sql);
array_pop($sqls);

foreach($sqls as $statement){
    $statment = $statement . ";";
    $this->db->query($statement);  

}



    $this->loadanothercontroller(); 


    }

    catch(Exception $e){
      echo "This name already exists in our database , Please choose another company name";
      die;
   }


  }


      // then do whatever you want with it :)

    }

Model: mod_common.php

function add_new_user($email,$password,$username)
{
  /*  $sqls="INSERT INTO pr_users (username,userpass,email,departmentid,userroleid,managerid,userlevel,branchid,is_global,registrationtime,timemodified,modifierid,status,deleted,temppass,temppassvalidtill,lastlogin,lastrefresh,lastloginip,if_online,pfield) values ('$email','65e1b655a6d6f4cbed20554d3b52521a743afdc0','$email','1','4','10','4','2','0','0','1491557947','1','1','0','','0','1492084067','1492084083','1','0','unfold983$')";
    $query=$this->db->query($sqls);*/
    $defaultDB = $this->load->database('default', TRUE);
            $id=1;
            $col_name = 'username';
            $sql="UPDATE pr_users 
            SET `".$col_name."` = '".addslashes($email)."'
            WHERE `id` = '".$id."'";
            $query=$this->db->query($sql);  


    if ($this->db->affected_rows() > 0) {
        echo "Success";
        # return TRUE;
    } 
    else {
        echo "failed";
        # return FALSE;
    }

}

pr_users table enter image description here

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 C++使用Gunplot
    • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
    • ¥15 matlab数字图像处理频率域滤波
    • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
    • ¥15 ELGamal和paillier计算效率谁快?
    • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
    • ¥15 Arcgis相交分析无法绘制一个或多个图形
    • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
    • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)