连接到另一个数据库并更新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

dow58115
dow58115 直接要求是看看这是否有效,SQL注入和MD5将在以后处理,这仅用于演示目的
3 年多之前 回复
duangangmo0583
duangangmo0583 由于一些其他约束,例如我们在其他代码中依赖于id=1,每个db都有一个单独的admin。
3 年多之前 回复
douzhongqiu5032
douzhongqiu5032 使用用户名为每个用户创建数据库?你到底想要做什么?!?
3 年多之前 回复
dq13733519262
dq13733519262 您的代码容易受到SQL注入攻击,而MD5不适用于散列密码。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐