Page 1 of 1

CLONE FIELDS TO ANOTHER TABLE

Posted: 2014-06-19 14:53
by espo
I tested this hooks:

Code: Select all

function table_after_insert($data, $memberInfo, &$args){

	$id = $data['id'];
    $nome = $data['nome'];
    $cognome = $data['cognome'];
    $preventivo = $data['preventivo'];
	
	mysql_connect("localhost", "admin", "password") or die(mysql_error()); 
    mysql_select_db("prova") or die(mysql_error()); 

    mysql_query( "INSERT INTO Amministrazione (nome, cognome, preventivo) VALUES ('$nome', '$cognome', '$preventivo')") or die(mysql_error());
		
		return TRUE;
	}
and it works, the fields are copied in new table.
But if I want create in table_after_update, i can't use this code, because if I use it, the fields are modified in the first table, but in the second table I have new fields. For to update the fields created, how can do?

Thank you very much.

Re: CLONE FIELDS TO ANOTHER TABLE

Posted: 2014-06-20 13:21
by espo
I solved in this mode and works:

Code: Select all

function Marketing_after_update($data, $memberInfo, &$args){

	$id = $data['id'];
    $nome = $data['nome'];
    $cognome = $data['cognome'];
    $preventivo = $data['preventivo'];
	
	mysql_connect("localhost", "admin", "xxxx") or die(mysql_error()); 
    mysql_select_db("prova") or die(mysql_error()); 

    
	mysql_query( "UPDATE Marketing, Amministrazione set Amministrazione.nome=Marketing.nome, 

Amministrazione.cognome=Marketing.cognome, Amministrazione.preventivo=Marketing.preventivo where 

Marketing.id=Amministrazione.id") or die(mysql_error()); 
    
	
		return TRUE;
	}
Two tables must have the same ID when it create the record in first table.

Now I would create the code DELETE. It's possible delete a record in the table number 1 and automaticaly delete the same record in the table number 2?

Re: CLONE FIELDS TO ANOTHER TABLE

Posted: 2014-06-21 13:48
by KSan
Take a look at the tool introduced in the following link

http://www.maatkit.org/doc/mk-table-sync.html

There are few other mysql synch solutions out there as well. This could be an option to consider. Rather than duplicating your insert/update/delete actions you could see if you can use a tool to synch up your whole table.