Inserting, editing and excluding registers in the database using AMFPHP

Already we know how Populating a Datagrid using AMFPHP, now we will see as to insert, to bring up to date and to exclude registers of the data base being used the AMFPHP We will use archive of the previous article the same .

We will not need to change in nothing our structure database. We will only modify the classroom in PHP and the FLA. Our class in PHP this:

[php]< ? class query{ var $host = "localhost"; var $user = 'root'; var $database = 'seubanco'; var $pass = 'suasenha'; function query(){ $this->methodTable = array(
“get_user”=>array(
“description”=>”populando nosso datagrid”,
“accessâ€?=>”remoteâ€?
)
);
$this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);
mysql_select_db($this->database);
}
function get_user(){
$query = mysql_query(“SELECT * FROM usuarios”);
if (!mysql_error()) {
return $query;
}else {
return mysql_error();
}
}
}?>[/php]

we will add the metodos to insert, to bring up to date and to exclude the registers of the data base, respectively setRecords, updateRecords and delRecords.
it is necessary adds them in the construction function, in the variable methodTable.
Our class was thus:

[php]< ? class query{ var $host = "localhost"; var $user = 'root'; var $database = 'seubanco'; var $pass = 'suasenha'; function query(){ $this->methodTable = array(
“get_user”=>array(
“description”=>”populando nosso datagrid”,
“access”=>”remote”
),
“setRecords”=> array(
“description”=> “Insere registros no banco de dados”,
“access”=> “remote”,
“arguments”=>array (“nome”,”email”,”telefone”)
),
“updateRecords”=> array(
“description”=> “Atualiza os registros do banco de dados”,
“access”=> “remote”,
“arguments”=>array (“id”,”nome”,”email”,”telefone”)
),
“delRecords”=> array(
“description”=> “exclui os registros do banco de dados”,
“access”=> “remote”,
“arguments”=>array (“id”)
)
);
$this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);
mysql_select_db($this->database);
}

function get_user(){
$query = mysql_query(“SELECT * FROM usuarios”);
if (!mysql_error()) {
return $query;
}else {
return mysql_error();
}
}
function setRecords($nome,$email,$telefone){
$query = mysql_query(“INSERT INTO usuarios (nome,email,telefone) VALUES (‘$nome’,’$email’,’$telefone’)”);
if (!mysql_error()) {
return “sucesso”;
}else{
return “error”;
}
}
function updateRecords($id,$nome,$email,$telefone){
$query = mysql_query(“UPDATE usuarios SET nome = ‘$nome’,email = ‘$email’, telefone = ‘$telefone’ WHERE id = ‘$id'”);
if (!mysql_error()) {
return “sucesso”;
}else {
return “error”;
}
}
function delRecords($id){
$query = mysql_query(“DELETE FROM usuarios WHERE id = ‘$id’ LIMIT 1”);
if (!mysql_error()) {
return “sucesso”;
}else{
return “error”;
}
}
}
?>[/php]

Now we go to our archive in Flash, remembering that it only will contain the codes, the components (generated dinamicamente script saw action) will have to be in the library of the swf.
We will dinamicamente create dinamicamente the fields of text and the buttons, using the method createClassObject, being necessary to specify the way of the component.
For example to create a button dinamicamente.

[as]_root.createClassObject(mx.controls.Button, “Button”, 1);[/as]

One another way to make this would be to import the classrooms of the component in the start of script.

[as]import mx.controls.Button;[/as]

and in metodo

[as]_root.createClassObject(Button, ” Button “, 1);[/as]

The second argument will be the name of the instancia of the object created in the case “Button�
Understood as the components they are created dinamicamente, we will need that when the option of datagrid will be clicked, that it appears in the fields of texts to be brought up to date or excluded. For this we will add to an object listener who listens to ours datagrid. We will make this of the function inside user_Result.

[as]var listenerObject:Object = new Object();[/as]

creating the function that to respond to the object listener, in it we will catch the option that will be chosen in datagrid to fill the fields of texts , it notices that id of datagrid will be stored in an variable to be later used in the metodos to bring up to date and to exclude the information of the database . We are including the actions (onRelease) that they will be used in the buttons to bring up to date and to exclude the registers . It notices that it is not being enclosed the action of the button to insert therefore will not depend on datagrid.
When the button to bring up to date will be clicado it will order for the server the parameters that will be in the field of text and the variable “idâ€? for method updateRecords(id, campo_nome.text, campo_email.text, campo_tel.text); (the same for exclusion ) e for I finish adding to everything this datagrid with the method addEventListener(“change”, listenerObject);

[as]listenerObject.change = function(evt) {
var id:Number = evt.target.selectedItem.id;
campo_nome.text = evt.target.selectedItem.nome;
campo_email.text = evt.target.selectedItem.email;
campo_tel.text = evt.target.selectedItem.telefone;
trace(evt.target.selectedItem.id);
bot_update.onRelease = function() {
var pcs:PendingCall = conexao.updateRecords(id, campo_nome.text, campo_email.text, campo_tel.text);
pcs.responder = new RelayResponder(_root, “updateRecords_Result”, “updateRecords_Fault”);
};
bot_excluir.onRelease = function() {
trace(id);
var pcs:PendingCall = conexao.delRecords(id);
pcs.responder = new RelayResponder(_root, “delRecords_Result”, “delRecords_Fault”);
};
};
grid.addEventListener(“change”, listenerObject);[/as]

will also use the component “Alert� to confirm that the insertion, update and exclusion of a register were made successfully. It will be called inside the functions setRecords_Result, updateRecords_Result and delRecords_Result, being that as soon as ok of alert will be clicado the button, will be invoked the method update_Dg that datagrid will bring up to date the data of ours.

[as]var dialog_obj:Object = Alert.show(“Sucess”, “Sucess!!!”, Alert.OK, null, update_Dg);[/as]

the code:

[as]//classes do Flash Remoting
import mx.remoting.Service;
import mx.services.Log;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.remoting.DataGlue;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.RecordSet;
import mx.controls.Alert;
//criando a conexão do flash remoting para o AMFPHP
//coloque o endereço do seu servidor e a sua classe
var conexao = new Service(“http://localhost/flashservices/gateway.php”, new Log(), “query”, null, null);
//chamando o metodo get_user()
var pcs:PendingCall = conexao.get_user();
pcs.responder = new RelayResponder(this, “user_Result”, “user_Fault”);
function user_Result(res:ResultEvent) {
var grid = _root.createClassObject(mx.controls.DataGrid, “dg”, 1, {_x:200, _y:0});
grid.setSize(350, 300);
grid.dataProvider = res.result;
var total = res.result.length;
var listenerObject:Object = new Object();
listenerObject.change = function(evt) {
var id:Number = evt.target.selectedItem.id;
campo_nome.text = evt.target.selectedItem.nome;
campo_email.text = evt.target.selectedItem.email;
campo_tel.text = evt.target.selectedItem.telefone;
trace(evt.target.selectedItem.id);
bot_update.onRelease = function() {
var pcs:PendingCall = conexao.updateRecords(id, campo_nome.text, campo_email.text, campo_tel.text);
pcs.responder = new RelayResponder(_root, “updateRecords_Result”, “updateRecords_Fault”);
};
bot_excluir.onRelease = function() {
var pcs:PendingCall = conexao.delRecords(id);
pcs.responder = new RelayResponder(_root, “delRecords_Result”, “delRecords_Fault”);
};
};
grid.addEventListener(“change”, listenerObject);
}
function user_Fault(fault:FaultEvent) {
trace(fault.fault.faultstring);
}
function setRecords_Result(res:ResultEvent) {
trace(res.result);
var dialog_obj:Object = Alert.show(“Sucess”, “Sucess!!!”, Alert.OK, null, update_Dg);
}
function setRecords_Fault(fault:FaultEvent) {
trace(fault.fault.faultstring);
}
function updateRecords_Result(res:ResultEvent) {
trace(res.result);
var dialog_obj:Object = Alert.show(“Sucess”, “Sucess!!!”, Alert.OK, null, update_Dg);
}
function updateRecords_Fault(fault:FaultEvent) {
trace(fault.fault.faultstring);
}
function delRecords_Result(res:ResultEvent) {
trace(res.result);
var dialog_obj:Object = Alert.show(“Sucess”, “Sucess!!!”, Alert.OK, null, update_Dg);
}
function delRecords_Fault(fault:FaultEvent) {
trace(fault.fault.faultstring);
}
var label_nome = _root.createClassObject(mx.controls.Label, “label_nome”, _root.getNextHighestDepth(), {_x:2, _y:5, text:”Nome:”});
var campo_nome = _root.createClassObject(mx.controls.TextInput, “campo_nome”, _root.getNextHighestDepth(), {_x:60, _y:5});
var label_email = _root.createClassObject(mx.controls.Label, “label_email”, _root.getNextHighestDepth(), {_x:2, _y:30, text:”Email:”});
var campo_email = _root.createClassObject(mx.controls.TextInput, “campo_email”, _root.getNextHighestDepth(), {_x:60, _y:30});
var label_tel = _root.createClassObject(mx.controls.Label, “label_tel”, _root.getNextHighestDepth(), {_x:2, _y:55, text:”Telefone:”});
var campo_tel = _root.createClassObject(mx.controls.TextInput, “campo_tel”, _root.getNextHighestDepth(), {_x:60, _y:55});
var bot_inserir:Button = _root.createClassObject(mx.controls.Button, “bot_inseir”, _root.getNextHighestDepth(), {_x:2, _y:100, label:”Inserir Registros”});
var bot_update:Button = _root.createClassObject(mx.controls.Button, “bot_update”, _root.getNextHighestDepth(), {_x:2, _y:130, label:”Atualizar Registro”});
var bot_excluir:Button = _root.createClassObject(mx.controls.Button, “bot_excluir”, _root.getNextHighestDepth(), {_x:2, _y:160, label:”Excluir Registro”});
bot_inserir.onRelease = function() {
var pcs:PendingCall = conexao.setRecords(campo_nome.text, campo_email.text, campo_tel.text);
pcs.responder = new RelayResponder(_root, “setRecords_Result”, “setRecords_Fault”);
};
function update_Dg():Void {
var pcs:PendingCall = conexao.get_user();
pcs.responder = new RelayResponder(_root, “user_Result”, “user_Fault”);
}
[/as]

view application

AMFPHP
http://www.amfphp.org/

Flash Remoting
http://www.macromedia.com/devnet/flashremoting/

1 thought on “Inserting, editing and excluding registers in the database using AMFPHP

Leave a Reply