CodeIgniter Forums
How to interact with another database and send the data also to another database? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to interact with another database and send the data also to another database? (/showthread.php?tid=69357)



How to interact with another database and send the data also to another database? - CodeigniterEX - 11-08-2017

Greetings, please show what code to write, so that after the transfer of money, he sent the data also to another database. Projects are located on the same server, they have different databases, how to implement this interaction? The Codeigniter framework is used, Code snippets:

PHP Code:
function start_transfer()
 
     
        
// get the data
 
       $user $this->users_model->get_user($this->user['id']);

 
       // Check fraud 
 
       if ($user['fraud_status']>0) {

 
           $this->session->set_flashdata('error'lang('users error fraud'));
 
           redirect(site_url("account/money_transfer"));

 
       }

 
           elseif ($user['fraud_status']==0) {

 
               $this->form_validation->set_rules('amount'lang('users transfer amount'), 'required|trim|numeric|greater_than[0]');
 
               $this->form_validation->set_rules('receiver'lang('users transfer amount'), 'required|trim|callback__check_username[]');
 
               $this->form_validation->set_rules('currency'lang('users transfer amount'), 'required|trim|in_list[debit_base,debit_extra1,debit_extra2,debit_extra3,debit_extra4,debit_extra5]');

 
               if ($this->form_validation->run() == FALSE)
 
                   {
 
                       $this->session->set_flashdata('error'lang('users error form'));
 
                       redirect(site_url("account/money_transfer"));
 
                   }
 
                   else
                    
{

 
                   $amount $this->input->post("amount");
 
                   $currency $this->input->post("currency");
 
                   $receiver $this->input->post("receiver");
 
                   $note $this->input->post("note");

 
                   $user_receiver $this->users_model->get_user_transfer($receiver);

 
                   $percent $this->settings->com_transfer/"100";
 
                   $fee $amount*$percent;
 
                   $sum $fee+$amount;

 
                   $total_receiver $user_receiver[$currency]+$amount;
 
                   $total_sender $user[$currency]-$sum;

 
                   // Check wallet
 
                   if ($user[$currency]<$sum) {

 
                       $this->session->set_flashdata('error'lang('users error wallet'));
 
                       redirect(site_url("account/money_transfer"));

 
                   }

 
                   elseif ($user[$currency]>$sum) {

 
                       // update receiver wallet
 
                       $this->users_model->update_wallet_transfer($receiver,
 
                           array(
 
                               $currency => $total_receiver,
 
                               )
 
                           );

 
                       // update sender wallet
 
                       $this->users_model->update_wallet_transfer($user['username'],
 
                       array(
 
                           $currency => $total_sender,
 
                           )
 
                       );

 
                       // add transaction for sender
 
                       $transactions $this->transactions_model->add_transaction(array(
 
                           "type"              => "3",
 
                           "sum"               => $sum,
 
                           "fee"               => $fee,
 
                           "amount"            => $amount,
 
                           "currency"          => $currency,
 
                           "status"            => "2",
 
                           "sender"            => $user['username'],
 
                           "receiver"          => $user_receiver['username'],
 
                           "time"              => date('Y-m-d H:i:s'),
 
                           "user_comment"      => $note,
 
                           "admin_comment"     => "none"
 
                           )
 
                       );




 
                       // set content data
 
                       $content_data = array(
 
                           'user'              => $user,
 
                           'user_receiver'     => $user_receiver,
 
                           'percent'           => $percent,
 
                       );

 
                       // Send payeer email

 
                       $email_template $this->emailtemplate_model->get_email_template(22);

 
                       // variables to replace
 
                       if ($currency=="debit_base") {
 
                           $mail_cyr $this->currencys->display->base_code;
 
                       } elseif ($currency=="debit_extra1") {
 
                           $mail_cyr $this->currencys->display->extra1_code;
 
                       } elseif ($currency=="debit_extra2") {
 
                           $mail_cyr $this->currencys->display->extra2_code;
 
                       } elseif ($currency=="debit_extra3") {
 
                           $mail_cyr $this->currencys->display->extra3_code;
 
                       } elseif ($currency=="debit_extra4") {
 
                           $mail_cyr $this->currencys->display->extra4_code;
 
                       } elseif ($currency=="debit_extra5") {
 
                           $mail_cyr $this->currencys->display->extra5_code;
 
                       

 
                       // variables to replace
 
                       $link site_url('account/history/');
 
                       $site_name $this->settings->site_name;
 
                       $mail_sum round($sum2);

 
                       $rawstring $email_template['message'];

 
                        // what will we replace
 
                       $placeholders = array('[SITE_NAME]''[SUM]''[CYR]''[URL_HISTORY]''[RECEIVER]');

 
                       $vals_1 = array($site_name$mail_sum$this->currencys->display->base_code$link$user_receiver['username']);

 
                       //replace
 
                       $str_1 str_replace($placeholders$vals_1$rawstring);

 
                       $this -> email -> from($this->settings->site_email$this->settings->site_name);
 
                       $this->email->to(
 
                           array($user['email'])
 
                       );

 
                       $this -> email -> subject($email_template['title']);

 
                       $this -> email -> message($str_1);

 
                       $this->email->send();

 
                       // variables to replace
 
                       if ($currency=="debit_base") {
 
                           $mail_cyr $this->currencys->display->base_code;
 
                       } elseif ($currency=="debit_extra1") {
 
                           $mail_cyr $this->currencys->display->extra1_code;
 
                       } elseif ($currency=="debit_extra2") {
 
                           $mail_cyr $this->currencys->display->extra2_code;
 
                       } elseif ($currency=="debit_extra3") {
 
                           $mail_cyr $this->currencys->display->extra3_code;
 
                       } elseif ($currency=="debit_extra4") {
 
                           $mail_cyr $this->currencys->display->extra4_code;
 
                       } elseif ($currency=="debit_extra5") {
 
                           $mail_cyr $this->currencys->display->extra5_code;
 
                       

 
                       $link site_url('account/history/');
 
                       $site_name $this->settings->site_name;

 
                       $rawstring $email_template['message'];

 
                       // what will we replace
 
                       $placeholders = array('[SITE_NAME]''[URL_HISTORY]''[SUM]''[CYR]');

 
                       $vals_1 = array($site_name$link$sum$mail_cyr);

 
                       //replace
 
                       $str_1 str_replace($placeholders$vals_1$rawstring);

 
                       $this -> email -> from($this->settings->site_email$this->settings->site_name);
 
                       $this->email->to(
 
                           array($user['email'])
 
                           );

 
                       $this -> email -> subject($email_template['title']);

 
                       $this -> email -> message($str_1);

 
                       $this->email->send();

 
                       $sms_template $this->smstemplate_model->get_sms_template(13);

 
                       if($sms_template['enable']) {

 
                           $rawstring $sms_template['message'];

 
                           // what will we replace
 
                           $placeholders = array('[SUM]''[CYR]');

 
                           $vals_1 = array($sum$mail_cyr);

 
                           //replace
 
                           $str_1 str_replace($placeholders$vals_1$rawstring);

 
                           // Twilio user number
 
                           $to '+'.$user['phone'];
 
                           // Your Account SID and Auth Token from twilio.com/console
 
                           $sid $this->settings->twilio_sid;
 
                           $token $this->settings->twilio_token;
 
                           $client = new Twilio\Rest\Client($sid$token);

 
                           // Use the client to do fun stuff like send text messages!
 
                           $client->messages->create(
 
                           // the number you'd like to send the message to
 
                           $to,
 
                               array(
 
                                   // A Twilio phone number you purchased at twilio.com/console
 
                                   'from' => '+'.$this->settings->twilio_number,
 
                                   // the body of the text message you'd like to send
 
                                   'body' => $str_1
                                
)
 
                           );

 
                       }


 
                       // Sending email receiver user

 
                       $email_template $this->emailtemplate_model->get_email_template(2);

 
                       // variables to replace
 
                       if ($currency=="debit_base") {
 
                           $mail_cyr $this->currencys->display->base_code;
 
                       } elseif ($currency=="debit_extra1") {
 
                           $mail_cyr $this->currencys->display->extra1_code;
 
                       } elseif ($currency=="debit_extra2") {
 
                           $mail_cyr $this->currencys->display->extra2_code;
 
                       } elseif ($currency=="debit_extra3") {
 
                           $mail_cyr $this->currencys->display->extra3_code;
 
                       } elseif ($currency=="debit_extra4") {
 
                           $mail_cyr $this->currencys->display->extra4_code;
 
                       } elseif ($currency=="debit_extra5") {
 
                           $mail_cyr $this->currencys->display->extra5_code;
 
                       

 
                       $link site_url('account/history/');
 
                       $site_name $this->settings->site_name;

 
                       $rawstring $email_template['message'];

 
                       // what will we replace
 
                       $placeholders = array('[SITE_NAME]''[URL_HISTORY]''[SUM]''[CYR]');

 
                       $vals_1 = array($site_name$link$amount$mail_cyr);

 
                       //replace
 
                       $str_1 str_replace($placeholders$vals_1$rawstring);

 
                       $this -> email -> from($this->settings->site_email$this->settings->site_name);
 
                       $this->email->to(
 
                           array($user_receiver['email'])
 
                       );

 
                       $this -> email -> subject($email_template['title']);

 
                       $this -> email -> message($str_1);

 
                       $this->email->send();

 
                       $sms_template2 $this->smstemplate_model->get_sms_template(12);

 
                       if($sms_template['enable']) {

 
                           $rawstring $sms_template2['message'];

 
                           // what will we replace
 
                           $placeholders = array('[SUM]''[CYR]');

 
                           $vals_1 = array($sum$mail_cyr);

 
                           //replace
 
                           $str_1 str_replace($placeholders$vals_1$rawstring);

 
                           // Twilio user number
 
                           $to '+'.$user_receiver['phone'];
 
                           // Your Account SID and Auth Token from twilio.com/console
 
                           $sid $this->settings->twilio_sid;
 
                           $token $this->settings->twilio_token;
 
                           $client = new Twilio\Rest\Client($sid$token);

 
                           // Use the client to do fun stuff like send text messages!
 
                           $client->messages->create(
 
                           // the number you'd like to send the message to
 
                               $to,
 
                                   array(
 
                                       // A Twilio phone number you purchased at twilio.com/console
 
                                       'from' => '+'.$this->settings->twilio_number,
 
                                       // the body of the text message you'd like to send
 
                                       'body' => $str_1
                                    
)
 
                               );
 
                       }
/* My code */
 
                       require './application/controllers/db.php';
 
                       $this->mysqli_query ("UPDATE slghani_users SET user_balance = user_balance+{$sum} WHERE user_login ='{$user['username']}'");
 
                       
/* End code */
$this->session->set_flashdata('message'lang('users transfer success'));
 
                       redirect(site_url("account/history"));

 
                   }

I wrote the followingbut it does not work
require 
'./application/controllers/db.php';
$this->mysqli_query ("UPDATE slghani_users SET user_balance = user_balance+{$sum} WHERE user_login ='{$user['username']}'");