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:

  1. < ?
  2. class query{
  3. var $host = "localhost";
  4. var $user = 'root';
  5. var $database = 'seubanco';
  6. var $pass = 'suasenha';
  7. function query(){
  8. $this->methodTable = array(
  9. "get_user"=>array(
  10. "description"=>"populando nosso datagrid",
  11. "access�=>"remote�
  12. )
  13. );
  14. $this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);
  15. mysql_select_db($this->database);
  16. }
  17. function get_user(){
  18. $query = mysql_query("SELECT * FROM usuarios");
  19. if (!mysql_error()) {
  20. return $query;
  21. }else {
  22. return mysql_error();
  23. }
  24. }
  25. }?>

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:

  1. < ?
  2.     class query{
  3.    var $host = "localhost";
  4.    var $user = 'root';
  5.    var $database = 'seubanco';
  6.    var $pass = 'suasenha';
  7.  
  8.     function query(){
  9.         $this->methodTable = array(
  10.         "get_user"=>array(
  11.         "description"=>"populando nosso datagrid",
  12.         "access"=>"remote"
  13.         ),
  14.         "setRecords"=> array(
  15.         "description"=> "Insere registros no banco de dados",
  16.         "access"=> "remote",
  17.         "arguments"=>array ("nome","email","telefone")
  18.         ),
  19.         "updateRecords"=> array(
  20.         "description"=> "Atualiza os registros do banco de dados",
  21.         "access"=> "remote",
  22.         "arguments"=>array ("id","nome","email","telefone")
  23.         ),
  24.         "delRecords"=> array(
  25.         "description"=> "exclui os registros do banco de dados",
  26.         "access"=> "remote",
  27.         "arguments"=>array ("id")
  28.         )
  29.         );
  30.         $this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);
  31.         mysql_select_db($this->database);
  32.     }
  33.  
  34.     function get_user(){
  35.         $query = mysql_query("SELECT * FROM usuarios");
  36.         if (!mysql_error()) {
  37.             return $query;
  38.         }else {
  39.             return mysql_error();
  40.         }
  41.     }
  42.     function setRecords($nome,$email,$telefone){
  43.         $query = mysql_query("INSERT INTO usuarios (nome,email,telefone) VALUES ('$nome','$email','$telefone')");
  44.         if (!mysql_error()) {
  45.             return "sucesso";
  46.         }else{
  47.             return "error";
  48.         }
  49.     }
  50.     function updateRecords($id,$nome,$email,$telefone){
  51.         $query = mysql_query("UPDATE usuarios SET nome = '$nome',email = '$email', telefone = '$telefone' WHERE id = '$id'");
  52.         if (!mysql_error()) {
  53.             return "sucesso";
  54.         }else {
  55.             return "error";
  56.         }
  57.     }
  58.     function delRecords($id){
  59.         $query = mysql_query("DELETE FROM usuarios WHERE id = '$id' LIMIT 1");
  60.         if (!mysql_error()) {
  61.             return "sucesso";
  62.         }else{
  63.             return "error";
  64.         }
  65.     }
  66. }
  67. ?>

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.

ActionScript

  1. _root.createClassObject(mx.controls.Button, "Button", 1);

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

ActionScript

  1. import mx.controls.Button;

and in metodo

ActionScript

  1. _root.createClassObject(Button, " Button ", 1);

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.

ActionScript

  1. var listenerObject:Object = new Object();

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);

ActionScript

  1. listenerObject.change = function(evt) {
  2. var id:Number = evt.target.selectedItem.id;
  3.         campo_nome.text = evt.target.selectedItem.nome;
  4.         campo_email.text = evt.target.selectedItem.email;
  5.         campo_tel.text = evt.target.selectedItem.telefone;
  6.         trace(evt.target.selectedItem.id);
  7.         bot_update.onRelease = function() {
  8.             var pcs:PendingCall = conexao.updateRecords(id, campo_nome.text, campo_email.text, campo_tel.text);
  9.             pcs.responder = new RelayResponder(_root, "updateRecords_Result", "updateRecords_Fault");
  10.         };
  11.         bot_excluir.onRelease = function() {
  12.             trace(id);
  13.             var pcs:PendingCall = conexao.delRecords(id);
  14.             pcs.responder = new RelayResponder(_root, "delRecords_Result", "delRecords_Fault");
  15.         };
  16.     };
  17. grid.addEventListener("change", listenerObject);

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.

ActionScript

  1. var dialog_obj:Object = Alert.show("Sucess", "Sucess!!!", Alert.OK, null, update_Dg);

the code:

ActionScript

  1. //classes do Flash Remoting
  2. import mx.remoting.Service;
  3. import mx.services.Log;
  4. import mx.rpc.RelayResponder;
  5. import mx.rpc.FaultEvent;
  6. import mx.remoting.DataGlue;
  7. import mx.rpc.ResultEvent;
  8. import mx.remoting.PendingCall;
  9. import mx.remoting.RecordSet;
  10. import mx.controls.Alert;
  11. //criando a conexão do flash remoting para o AMFPHP
  12. //coloque o endereço do seu servidor e a sua classe
  13. var conexao = new Service("http://localhost/flashservices/gateway.php", new Log(), "query", null, null);
  14. //chamando o metodo get_user()
  15. var pcs:PendingCall = conexao.get_user();
  16. pcs.responder = new RelayResponder(this, "user_Result", "user_Fault");
  17. function user_Result(res:ResultEvent) {
  18.     var grid = _root.createClassObject(mx.controls.DataGrid, "dg", 1, {_x:200, _y:0});
  19.     grid.setSize(350, 300);
  20.     grid.dataProvider = res.result;
  21.     var total = res.result.length;
  22.     var listenerObject:Object = new Object();
  23.     listenerObject.change = function(evt) {
  24.         var id:Number = evt.target.selectedItem.id;
  25.         campo_nome.text = evt.target.selectedItem.nome;
  26.         campo_email.text = evt.target.selectedItem.email;
  27.         campo_tel.text = evt.target.selectedItem.telefone;
  28.         trace(evt.target.selectedItem.id);
  29.         bot_update.onRelease = function() {
  30.             var pcs:PendingCall = conexao.updateRecords(id, campo_nome.text, campo_email.text, campo_tel.text);
  31.             pcs.responder = new RelayResponder(_root, "updateRecords_Result", "updateRecords_Fault");
  32.         };
  33.         bot_excluir.onRelease = function() {
  34.             var pcs:PendingCall = conexao.delRecords(id);
  35.             pcs.responder = new RelayResponder(_root, "delRecords_Result", "delRecords_Fault");
  36.         };
  37.     };
  38.     grid.addEventListener("change", listenerObject);
  39. }
  40. function user_Fault(fault:FaultEvent) {
  41.     trace(fault.fault.faultstring);
  42. }
  43. function setRecords_Result(res:ResultEvent) {
  44.     trace(res.result);
  45.     var dialog_obj:Object = Alert.show("Sucess", "Sucess!!!", Alert.OK, null, update_Dg);
  46. }
  47. function setRecords_Fault(fault:FaultEvent) {
  48.     trace(fault.fault.faultstring);
  49. }
  50. function updateRecords_Result(res:ResultEvent) {
  51.     trace(res.result);
  52.     var dialog_obj:Object = Alert.show("Sucess", "Sucess!!!", Alert.OK, null, update_Dg);
  53. }
  54. function updateRecords_Fault(fault:FaultEvent) {
  55.     trace(fault.fault.faultstring);
  56. }
  57. function delRecords_Result(res:ResultEvent) {
  58.     trace(res.result);
  59.     var dialog_obj:Object = Alert.show("Sucess", "Sucess!!!", Alert.OK, null, update_Dg);
  60. }
  61. function delRecords_Fault(fault:FaultEvent) {
  62.     trace(fault.fault.faultstring);
  63. }
  64. var label_nome = _root.createClassObject(mx.controls.Label, "label_nome", _root.getNextHighestDepth(), {_x:2, _y:5, text:"Nome:"});
  65. var campo_nome = _root.createClassObject(mx.controls.TextInput, "campo_nome", _root.getNextHighestDepth(), {_x:60, _y:5});
  66. var label_email = _root.createClassObject(mx.controls.Label, "label_email", _root.getNextHighestDepth(), {_x:2, _y:30, text:"Email:"});
  67. var campo_email = _root.createClassObject(mx.controls.TextInput, "campo_email", _root.getNextHighestDepth(), {_x:60, _y:30});
  68. var label_tel = _root.createClassObject(mx.controls.Label, "label_tel", _root.getNextHighestDepth(), {_x:2, _y:55, text:"Telefone:"});
  69. var campo_tel = _root.createClassObject(mx.controls.TextInput, "campo_tel", _root.getNextHighestDepth(), {_x:60, _y:55});
  70. var bot_inserir:Button = _root.createClassObject(mx.controls.Button, "bot_inseir", _root.getNextHighestDepth(), {_x:2, _y:100, label:"Inserir Registros"});
  71. var bot_update:Button = _root.createClassObject(mx.controls.Button, "bot_update", _root.getNextHighestDepth(), {_x:2, _y:130, label:"Atualizar Registro"});
  72. var bot_excluir:Button = _root.createClassObject(mx.controls.Button, "bot_excluir", _root.getNextHighestDepth(), {_x:2, _y:160, label:"Excluir Registro"});
  73. bot_inserir.onRelease = function() {
  74.     var pcs:PendingCall = conexao.setRecords(campo_nome.text, campo_email.text, campo_tel.text);
  75.     pcs.responder = new RelayResponder(_root, "setRecords_Result", "setRecords_Fault");
  76. };
  77. function update_Dg():Void {
  78.     var pcs:PendingCall = conexao.get_user();
  79.     pcs.responder = new RelayResponder(_root, "user_Result", "user_Fault");
  80. }

view application

AMFPHP
http://www.amfphp.org/

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

Was this article helpful? feel free to make a donation and help keep the blog in the air
AMFPHP, Flash, PHP

Leave a Reply