5 Replies Latest reply on Sep 22, 2006 12:17 PM by leotemp

    Just couldn't figure out flex with php to do a web application

    J-Girl
      whats wrong with my things?
      can someone help me!
      i just cant figure why my web application cant communicate with my database.

      My MXML code:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" creationComplete="addRecord.send()">
      <mx:HTTPService id="addRecord" url=" http://localhost/flex/php/do_addrecord.php" useProxy="false" method="POST">
      <mx:request xmlns="">
      <id>100</id><note>abc</note>
      </mx:request>
      </mx:HTTPService>
      <mx:Form x="22" y="10" width="493">

      <mx:Button label="Submit" click="addRecord"/>
      </mx:Form>
      <mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{addRecord.lastResult.test_table.tests}">
      <mx:columns>
      <mx:DataGridColumn headerText="ID" dataField="id"/>
      <mx:DataGridColumn headerText="Note" dataField="note"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Application>



      My do_addrecord.PHP code:

      <?php
      $db_name = "test";
      $table_name = "test_table";
      $connection = @mysql_connect("localhost","spike","123mpower")
      or die(mysql_error());
      $db = @mysql_select_db($db_name, $connection) or
      die(mysql_error());
      $sql = "INSERT INTO $table_name (id, note) VALUES ($_POST[id], '$_POST[note]')";
      ?>
      <HTML>
      <HEAD>
      <TITLE>Add a Record</TITLE>
      </HEAD>
      <BODY>
      <H1>Adding a Record to <? echo "$table_name"; ?></H1>
      </BODY>
      </HTML>




      please help me on this database thing.. i highly need to interact with my database to do select statements and etc...
        • 1. Re: Just couldn't figure out flex with php to do a web application
          peterent Level 2
          My guess is that your PHP script file name is not matching. Your HTTPService url is do_addrecord.php and below you say the file name is do_addrecord.PHP - the extensions are different.

          Also make sure you can run the PHP script from a browser (you'll need to use $_GET instead of $_POST for the test). The url you enter from the browser should be the same one in the HTTPService. Once you get the PHP script to work from the browser it will work from Flex.

          I would also make the PHP script simply echo "OK" if the insert works or echo "ERROR" if it fails. When using HTTPService with Flex you don't usually return HTML because the HTML tags are meaningless to Flex - you should just return data (eg, XML if you are running a query; simple text for inserts, etc.).
          • 2. Re: Just couldn't figure out flex with php to do a web application
            J-Girl Level 1
            thanks peterent~
            but anyway the .php extension thing is the same..
            but i dun know how to do what you say..
            i went to change all $_POST to $_GET and type the HTTPService that url on my ie but nothing came out..
            i dont know did i did the right thing..

            anyway i got this from web but also cant work...


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" creationComplete="send_data()">
            <mx:Script>
            <![CDATA[
            private function send_data():void {
            userRequest.send();
            }
            ]]>
            </mx:Script>
            <mx:Form x="22" y="10" width="493">
            <mx:HBox>
            <mx:Label text="Username"/>
            <mx:TextInput id="username"/>
            </mx:HBox>
            <mx:HBox>
            <mx:Label text="Email Address"/>
            <mx:TextInput id="emailaddress"/>
            </mx:HBox>
            <mx:Button label="Submit" click="send_data()"/>
            </mx:Form>
            <mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{userRequest.lastResult.users.user}">
            <mx:columns>
            <mx:DataGridColumn headerText="User ID" dataField="userid"/>
            <mx:DataGridColumn headerText="User Name" dataField="username"/>
            </mx:columns>
            </mx:DataGrid>
            <mx:TextInput x="22" y="292" id="selectedemailaddress" text="{dgUserRequest.selectedItem.emailaddress}"/>
            <mx:HTTPService id="userRequest" url=" http://localhost/flex/php/request.php" useProxy="false" method="POST">
            <mx:request xmlns="">
            <username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>
            </mx:request>
            </mx:HTTPService>
            </mx:Application>



            <?php
            //connect to the database
            $mysql = mysql_connect(localhost, "user", "password");

            mysql_select_db( "sample" );
            //if the username and email address are filled out
            if( $_POST["emailaddress"] AND $_POST["username"])
            {
            //add the user
            $Query = "INSERT INTO users VALUES ('', '".$_POST['username']."', '".$_POST['emailaddress']."')";

            $Result = mysql_query( $Query );
            }

            //return a list of all the users
            $Query = "SELECT * from users";
            $Result = mysql_query( $Query );

            $Return = "<users>";

            while ( $User = mysql_fetch_object( $Result ) )
            {
            $Return .= "<user><userid>".$User->userid."</userid><username>".$User->username."</username><emailad dress>".$User->emailaddress."</emailaddress></user>";
            }
            $Return .= "</users>";
            mysql_free_result( $Result );
            print ($Return)
            ?>



            i changed the user to my username and password to my password ...
            i have the database name sample and a table name 'users'..
            the fields (columns) in the 'users' table consists of 3 columns - userid, username, emailaddress ..
            the form came out well but the url of the ie shows from c:\ drive... but i tried typing http://localhost/Test.html or Test.mxml it appear htpp 404 not found.. or isit some webserver thing.. i just installed apache because xp home dont have IIS...

            Hope to have some help.. thanks
            anyway i'm new to Flex ..
            • 3. Re: Just couldn't figure out flex with php to do a web application
              ntsiii Level 3
              You must get the php side working first. If nothing came out then you still have a problem.

              Do this one step at a time. Make sure you can build a php page that returns data to the browser.

              Tracy
              • 4. Re: Just couldn't figure out flex with php to do a web application
                ntsiii Level 3
                Change your php page to just return some xml first.

                Then get it to read the querystring arguments and build them into the response.

                Then hook up the database.

                Then call it from Flex

                Tracy
                • 5. Re: Just couldn't figure out flex with php to do a web application
                  leotemp Level 1
                  make sure the php script is properly returning your xml and that it is well formed. access the script through your browsers address bar and make sure your requesting querstrings for testing purposes, after you have verified your data is 100% then worry about flex, 9 times out of 10 my asp file is incorrect not my flex app.