18 Replies Latest reply on Mar 24, 2010 10:02 AM by Matt Le Fevre

    Don't know how to connect to phpMyAdmin from flex

    alcaponis

      Hi,

      I am a new guy to flex and phpMyAdmin.

      I have a php file named "myTest,php", have a wamp server and a flex project.

      I want to connect to do an aplication that connects to my data base, checks text from textinputs for user and passwords and shows me alert with text "login successful or failed" but everytime i run this application it shows me "login failed". I dont know what is the problem couse it not shows any errors. Maybe you could help me? Tell me what am i doing wrong?

       

      (hope that you understood what I want to do)

      sorry for my english...

       

      myTest,php:

      <?PHP

      echo "<?xml version=\"1.0\"?>\n";

       

      $hostname_conn = "localhost";

      $database_conn = "test";

      $username_conn = "root";

      $password_conn = "";

      $conn = mysql_connect($hostname_conn, $username_conn, $password_conn);

      if (!$conn) {

      die("not connected :" . mysql_error());

      }

       

      $db_selected = mysql_select_db($database_conn, $conn);

      if (!$db_selected)

      {

      die("not connected :" . mysql_error());

      }

       

      $query="UPDATE login SET password='veikia' WHERE username='Admin'";

      $testresult=mysql_query($query)

       

      $user = $_Post('user');

      $pass = $_Post('pass');

       

      $results = mysql_query("SELECT * FROM login where 1");

      $loged = false;

       

      while ($row = mysql_fetch_assoc($results));

      {

      if (strtolower($user) == strtolower($row['username']) &&

      strtolower($pass) == strtolower($row['password']))

      {

      $loged = true;

      }

      }

       

      if ($loged == true)

      echo "<status>true</status>"

      else

      echo "<status>true</status>"

       

      ?>

       

      Flex application named login.mxml

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle">


      <mx:Script>

      <![CDATA[

      import mx.rpc.events.ResultEvent;

      import mx.controls.Alert;

       

      private function logintest():void{

      loginService.send();

      }

       

      private function onResult(e:ResultEvent):void{

      if (e.result.status == true)

      Alert.show("Login successful", "Success");

      else

      Alert.show("Login failedl", "Failure");

      }

      ]]>

      </mx:Script>


      <mx:HTTPService id="loginService" url="D:\Flex workspace\wamp\www\myTest.php" method="post" result="onResult(event)">

      <mx:request xmlns="">

      <user>{userEdit.text}</user>

      <pass>{passEdit.text}</pass>

      </mx:request>

      </mx:HTTPService>


      <mx:Panel title="LogIn">

      <mx:VBox>

      <mx:HBox>

      <mx:Label text="user"/>

      <mx:TextInput id="userEdit"/>

      </mx:HBox>

      <mx:HBox>

      <mx:Label text="pass"/>

      <mx:TextInput id="passEdit"/>

      </mx:HBox>

      <mx:Button label="login" click="logintest()"/>

      </mx:VBox>

      </mx:Panel>

       

      </mx:Application>

      Maybe I have to put myTest.php file somewhere else?
      Please help

        • 1. Re: Don't know how to connect to phpMyAdmin from flex
          Gregory Lafrance Level 6

          Trace the result and see what you are getting back.

           

          Maybe you are getting back something indicating login passed but you may be parsing it wrong in the result handler.

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          • 2. Re: Don't know how to connect to phpMyAdmin from flex
            alcaponis Level 1

            So when I debug it, I get:

            e.result.status [0] = true, [1] = true.

            So that means everything is working? But my code in application is wrong?

            • 3. Re: Don't know how to connect to phpMyAdmin from flex
              Gregory Lafrance Level 6

              Try this:

               

              if (e.result.status == "true")

               

              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              • 4. Re: Don't know how to connect to phpMyAdmin from flex
                alcaponis Level 1

                Its still does nothing.

                I noticed that e.result.status is allways == true, nomatter what what I send to server. So it must be something wrong in the myTest,php file?

                • 5. Re: Don't know how to connect to phpMyAdmin from flex
                  Matt Le Fevre Level 4

                  Quickly did a tidy and checked it over, try the following

                   

                  flex code:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle">
                  
                  
                      <mx:Script>
                      
                      <![CDATA[
                          
                          import mx.rpc.events.ResultEvent;
                          import mx.controls.Alert;
                          
                          [Bindable]
                          public var username:String;
                          [Bindable]
                          public var password:String;
                          
                          private function logintest():void
                          {
                              username = userEdit.text;
                              password = passEdit.text;
                              loginService.send();
                          }
                          
                          private function onResult(e:ResultEvent):void
                          {
                              if (e.result.status == true)
                              {
                                  Alert.show("Login successful", "Success");
                              }            
                              else
                              {
                                  Alert.show("Login failedl", "Failure");
                              }
                          }
                          
                      ]]>
                      
                      </mx:Script>
                      
                      
                      <mx:HTTPService id="loginService" url="D:\Flex workspace\wamp\www\myTest.php" method="POST" result="onResult(event)">
                      <mx:request xmlns="">
                          <user>{username}</user>
                          <pass>{password}</pass>
                      </mx:request>
                      </mx:HTTPService>    
                      
                      <mx:Panel title="LogIn">
                          <mx:VBox>
                              <mx:HBox>
                                  <mx:Label text="user"/>
                                  <mx:TextInput id="userEdit"/>
                              </mx:HBox>
                              <mx:HBox>
                                  <mx:Label text="pass"/>
                                  <mx:TextInput id="passEdit"/>
                              </mx:HBox>
                          <mx:Button label="login" click="logintest()"/>
                          </mx:VBox>
                      </mx:Panel>
                      
                  </mx:Application>
                  

                   

                  PHP code:

                   

                  <?PHP
                  
                       $hostname_conn = "localhost";
                       $username_conn = "root";
                  
                       $password_conn = "";
                  
                       $conn = mysql_connect($hostname_conn, $username_conn, $password_conn);
                  
                       mysql_select_db("test");
                  
                       $user = mysql_real_escape_string($_POST["user"]);
                       $pass = mysql_real_escape_string($_POST["pass"]);
                  
                       $query = "SELECT * FROM login WHERE user = '$user' AND password = '$pass'";
                  
                       $result = mysql_query($query);
                       
                       $logged = mysql_num_rows($result);
                       
                       if ($logged == 1)
                       {
                            echo "<status>true</status>";
                       }
                       else
                       {
                            echo "<status>false</status>";
                       }
                   
                  ?>
                  1 person found this helpful
                  • 6. Re: Don't know how to connect to phpMyAdmin from flex
                    alcaponis Level 1

                    Now in the browser shows me those errors:

                     

                    [RPC Fault faultString="Default decoder could not decode result" faultCode="Client.CouldNotDecode" faultDetail="null"]

                    at mx.rpc.http::HTTPService/http://www.adobe.com/2006/flex/mx/internal::processResult()[C:\autobuild\3.2.0\frameworks\ projects\rpc\src\mx\rpc\http\HTTPService.as:881]

                    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\ projects\rpc\src\mx\rpc\AbstractInvoker.as:188]

                    at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responde r.as:43]

                    at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\ AsyncRequest.as:74]

                    at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\s rc\mx\messaging\channels\DirectHTTPChannel.as:403]

                    at flash.events::EventDispatcher/dispatchEventFunction()

                    at flash.events::EventDispatcher/dispatchEvent()

                    at flash.net::URLLoader/onComplete()

                     

                     

                    • 7. Re: Don't know how to connect to phpMyAdmin from flex
                      Matt Le Fevre Level 4

                      try changing this line

                       

                      <mx:HTTPService id="loginService" url="D:\Flex workspace\wamp\www\myTest.php" method="POST" result="onResult(event)">

                       

                      to

                       

                      <mx:HTTPService id="loginService" url="http://localhost/myTest.php" method="POST" result="onResult(event)">
                      • 8. Re: Don't know how to connect to phpMyAdmin from flex
                        alcaponis Level 1

                        Now I changed service url just to myTest,php and in debug mode it returns status = false.

                        Now it is just something wrong with a flex code

                        • 9. Re: Don't know how to connect to phpMyAdmin from flex
                          Matt Le Fevre Level 4

                          alcaponis wrote:

                           

                          Now I changed service url just to myTest,php and in debug mode it returns status = false.

                          Now it is just something wrong with a flex code

                           

                           

                          maybe, i've altered your PHP a fair bit.

                           

                          basically all it does now is check to see if the username/password submitted in flex exists in your database.

                           

                           

                          what were you trying to do before with this:

                           

                           

                          $query="UPDATE login SET password='veikia' WHERE username='Admin'";
                          $testresult=mysql_query($query)
                          

                           

                          ?

                          • 10. Re: Don't know how to connect to phpMyAdmin from flex
                            alcaponis Level 1

                            Now it is always false ;(

                            • 11. Re: Don't know how to connect to phpMyAdmin from flex
                              alcaponis Level 1

                              with this... I saw in tutorial that when you run php file it changes the password to "veikia" and you can see that your php file is working, but it does not change anything in my database...

                              Maybe there is something wrong with my browser or something? I have never do that before, so maybe I have to change something in the explorer? Or something?

                              • 12. Re: Don't know how to connect to phpMyAdmin from flex
                                Matt Le Fevre Level 4

                                alcaponis wrote:

                                 

                                with this... I saw in tutorial that when you run php file it changes the password to "veikia" and you can see that your php file is working, but it does not change anything in my database...

                                Maybe there is something wrong with my browser or something? I have never do that before, so maybe I have to change something in the explorer? Or something?

                                 

                                 

                                Yeah basically your old PHP file would change the user "Admin" password to 'veikia' each time it was run. I don't really understand the point of that!!!

                                 

                                 

                                Anyways, tell me exactly what you want this program to do and i'll try to help

                                • 13. Re: Don't know how to connect to phpMyAdmin from flex
                                  alcaponis Level 1

                                  First with that password changing...

                                  If it would change, I could see that myTest,php somehow works.

                                  But now when it isnt changing the password, i can see that it is not working. Did you get the poing?

                                   

                                  The truth is that I dont even know the php language.. So Im green as grass at summer

                                   

                                  The second what I want to do with this project is:

                                   

                                  1. Lean how to connect to database

                                  2. Get the data from database

                                  3. Change the data in database.

                                   

                                  But now it is just going to database and looks is the username and the password is correct...

                                   

                                  It is very hard to learn when every day I try to do something it goes wrong... (((

                                  • 14. Re: Don't know how to connect to phpMyAdmin from flex
                                    alcaponis Level 1

                                    Can you explain to me how this works? :

                                    $user = mysql_real_escape_string($_POST["user"]);

                                    $pass = mysql_real_escape_string($_POST["pass"]);

                                    ...    

                                         $logged = mysql_num_rows($result);

                                     

                                     

                                    why this: mysql_real_escape_string,

                                    and why mysql_num_row?

                                    • 15. Re: Don't know how to connect to phpMyAdmin from flex
                                      Matt Le Fevre Level 4

                                      Can you explain to me how this works? :

                                       

                                       

                                      $user = mysql_real_escape_string($_POST["user"]);

                                      $pass = mysql_real_escape_string($_POST["pass"]);

                                       

                                      mysql_real_escape_string() is a pre-defined function in PHP.

                                       

                                      It's a standard security feature, you should do to all POST'ed data you pass to PHP scripts, it prevents people from SQL injecting your database and bypassing login systems and such.

                                       

                                      Can you explain to me how this works? :

                                       


                                           $logged = mysql_num_rows($result);

                                       

                                      mysql_num_rows() counts the number of rows that have been returned by a query. In this instance, you want to see if the users login/password exist in the database.

                                       


                                      Obviously you'll never have 2 people with the same password/username.

                                       

                                       

                                      So all this function does is check that a users name and password exist in the database.

                                       

                                      If it does, there will be 1 row of results, if not there will be 0 rows of results.

                                       

                                       

                                      I was going to write a brief login tutorial anyway, i'll post up the code for you to play around with.

                                      1 person found this helpful
                                      • 16. Re: Don't know how to connect to phpMyAdmin from flex
                                        alcaponis Level 1

                                        Thank you very much!

                                         

                                        I will try to find whats wrong with my app...

                                        • 17. Re: Don't know how to connect to phpMyAdmin from flex
                                          alcaponis Level 1

                                          So....

                                          I figured it out... I reinstalled WAMP and it worked.... I think there was something wrong with WAMP... Dont know what but now it is working and Im very happy!!! Yeah!!!

                                           

                                          Thak you all very much for your help!!!

                                           

                                          Thanks to ADOBE for creating this forum =D

                                          • 18. Re: Don't know how to connect to phpMyAdmin from flex
                                            Matt Le Fevre Level 4

                                            i have a working example & source code up here now

                                             

                                            http://www.mattlefevre.com/viewExample.php?tut=flexPHP&proj=Simple%20Login%20Application

                                             

                                             

                                            source code shows you how to display information from the database, and add new stuff to it