Php 连接到另一个db并更新codeigniter中的一行

Php 连接到另一个db并更新codeigniter中的一行,php,mysql,codeigniter,codeigniter-2,codeigniter-3,Php,Mysql,Codeigniter,Codeigniter 2,Codeigniter 3,我正试图从admin@something.com添加到$username中的字符串,但正在更新默认数据库,我想连接到新创建的数据库并更新pr_users表中id=1的用户名字段,但无法连接到新创建的数据库并更新该字段 我正在调用一个模型来更新表,即使用$data['applicator\u rows']=$this->mod\u common->add\u new\u user($email,$userid,$username)不知何故,foreach($sqls as$statement)后面

我正试图从
admin@something.com
添加到$username中的字符串,但正在更新默认数据库,我想连接到新创建的数据库并更新pr_users表中id=1的用户名字段,但无法连接到新创建的数据库并更新该字段

我正在调用一个模型来更新表,即使用
$data['applicator\u rows']=$this->mod\u common->add\u new\u user($email,$userid,$username)不知何故,foreach($sqls as$statement)后面的语句不起作用,请帮助

以下是我的控制器代码:

控制器: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 :)

    }
模型: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_用户表

您的代码易受SQL注入攻击,MD5不用于哈希密码。是否使用用户名为每个用户创建数据库?你到底想用它做什么?!?每个数据库都有一个单独的管理员,这是由于其他一些限制,例如我们在其他代码中依赖于id=1。直接的要求是看看这是否有效,SQL注入和MD5将在稍后处理,这仅用于演示目的