Nitin Jain
Nitin Jain

Reputation: 93

insert data into two tables of different databases on same instance and same user

I am currently inserting below data in brokers3_finalmain database in phpfox_user and phpfox_user_custom but now i want to insert the data full_name and country_iso in other database mobile at the same time .

public function delete_mobile($data)
{
    $params = array( ':mobile' => $data);
    $sql = "Delete FROM mobile where mobile=:mobile";
    $stmt = parent::query($sql, $params);

}

public function update_profile($data)
{
    $sql = 'UPDATE phpfox_user set full_name=:full_name, email=:email where user_id=:user_id';
    $params =(array(
        ':full_name' => $data['full_name'],
        ':email' => $data['email'],
        ':user_id' => $data['user_id']              
    ));
    //print_array($params);

    parent::query($sql, $params, '', 'main') or die(parent::$dbh->errorInfo());
}

public function register_user($data)
{
    //print_r($data);
    //$data=$data[0];
    //print_r($data);
    $params = array( ':email' => $data['email'], ':cf_mobile' => $data['mobile']);
    $sql = "SELECT * FROM phpfox_user, phpfox_user_custom where phpfox_user.email=:email AND phpfox_user_custom.cf_mobile=:cf_mobile";
    $stmt = parent::query($sql, $params, '', 'main');
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
        $res=$row;
    endwhile;

    if(count($res)>=1)
    {
        $result['status']='fail';
        $result['message']='user already exits';
        $result['user_id']=$res['user_id'];
    } else {
        $pass=md5(md5($data['password']).md5('78>'));
        $sql = 'INSERT INTO phpfox_user (full_name, email, password, password_salt, user_group_id, country_iso) 
        VALUES(:full_name, :email, :password, :password_salt, :user_group_id, :country_iso)';

        $params =(array(
            ':full_name' => $data['full_name'],
            ':email' => $data['email'],
            ':password' => $pass,
            ':password_salt' => '"Q>',
            ':user_group_id' => $data['user_group_id'],
            ':country_iso' => $data['location']             
        ));
        //print_array($params);

        parent::query($sql, $params, '', 'main') or die(parent::$dbh->errorInfo());
        # Affected Rows?
        $user_id=parent::$dbh->lastInsertId();

        $sql = 'INSERT INTO phpfox_user_custom (user_id, cf_mobile) 
        VALUES(:user_id, :cf_mobile)';
        $params =(array(
            ':user_id' => $user_id,
            ':cf_mobile' => $data['mobile']
        ));
        //print_array($params);

        parent::query($sql, $params, '', 'main') or die(parent::$dbh->errorInfo());
        # Affected Rows?
        //return parent::$dbh->lastInsertId();
        //return $user_id;
        $result['status']='success';
        $result['message']='Registeration successfully';
        $result['user_id']=$user_id;
    }
    return $result;
}

Upvotes: 0

Views: 1128

Answers (1)

Max
Max

Reputation: 2643

If both DB reside on the same mysql server and you establish the connection without picking a database, just put the name of the database in front of the tablename like

INSERT INTO myDBname.myTableName (myKey) VALUES (:myKey);

If you have a connection to one db you can change it before inserting.

$pdoConnetion->select_db('myDBName');
INSERT INTO myTableName (myKey) VALUES (:myKey);

Upvotes: 1

Related Questions