3 Replies Latest reply on Feb 27, 2010 5:53 AM by naval joshi

    connecting flex with php(wamp)

    naval joshi

      i m trying to connect a flex application to a php (wamp server)..bt i m unable to do soo

      ma application runs fine....bt wen i click submit button it shows ..transfering data frm localhost n nothing happens......database is nt updated........
      //flex code

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="send_data()">
      <mx:Script>
      <![CDATA[
      import mx.controls.DataGrid

       

      private function send_data():void {
      userRequest.send();
      }
      ]]>
      </mx:Script>

       

      <mx:Label x="79" y="89" text="username"/>
      <mx:Label x="79" y="128" text="rolnumber"/>
      <mx:TextInput id="nm" x="153" y="87"/>
      <mx:TextInput x="153" y="126" id="roll"/>
      <mx:Button x="127" y="191" label="submit" id="sub" click="send_data()"/>

       


      <mx:HTTPService id="userRequest" url="http://localhost/connect.php" useProxy="false" method="POST">

      <mx:request xmlns="">
      <username>{nm.text}</username><rollnumber>{roll.text}</rollnumber>
      </mx:request>
      </mx:HTTPService>

       


      </mx:Application>

       

       

       

      //php code

       

      <?php
      mysql_connect("localhost", "root", "") or die("cannot connect");
      mysql_select_db("std_db") or die("cannot select database");

       

      $q="INSERT INTO `std_db`.`student` (`name` ,`roll`)VALUES ( '$_POST[nm]', $_POST[roll])";
      if(mysql_query($q))
      echo "sucessfully inserted";
      else
      echo" error occured";
      //if($row=mysql_fetch_array($result))
      //{
      //echo $row['

       

      ?>

      //

        • 1. Re: connecting flex with php(wamp)
          Matt Le Fevre Level 4

          //php code

           

          <?php
          mysql_connect("localhost", "root", "") or die("cannot connect");
          mysql_select_db("std_db") or die("cannot select database");

           

          $q="INSERT INTO `std_db`.`student` (`name` ,`roll`)VALUES ( '$_POST[nm]', $_POST[roll])";
          if(mysql_query($q))
          echo "sucessfully inserted";
          else
          echo" error occured";
          //if($row=mysql_fetch_array($result))
          //{
          //echo $row['

           

          ?>

          //

           

          your query looks a little off, instead of

           

          mysql_connect("localhost", "root", "") or die("cannot connect");
          mysql_select_db("std_db") or die("cannot select database");

           

          $q="INSERT INTO `std_db`.`student` (`name` ,`roll`)VALUES ( '$_POST[nm]', $_POST[roll])";

           

          try

           

              mysql_connect("localhost", "root", "") or die("cannot connect");
              mysql_select_db("std_db") or die("cannot select database");
          
              $name = mysql_real_escape_string($_POST["username"]);
              $roll =    mysql_real_escape_string($_POST["rollnumber"]);
          
              $q = "INSERT INTO student (name , roll) VALUES ('$name', '$roll')";
          
          • 2. Re: connecting flex with php(wamp)
            naval joshi Level 1

            thanx a lot it worked........wat shd be the code fr display full datbasee nnnnnnshd i usee the datagrid?

            • 3. Re: connecting flex with php(wamp)
              Matt Le Fevre Level 4

              naval joshi wrote:

               

              thanx a lot it worked........wat shd be the code fr display full datbasee nnnnnnshd i usee the datagrid?

               

              you can use a datagrid if you want, or a tile list, or a list, or an advanced datagrid, it's entirely up to you and what you plan to do with the data.

               

               

              The Full working basic code for what you're trying to do is this:

               

               

              Flex code:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="send_data()">
                  
                  
                  <mx:Script>
                  <![CDATA[
                      import mx.rpc.events.ResultEvent;
                      import mx.controls.DataGrid
              
                      private function send_data():void 
                      {
                          userRequest.send();
                      }
                      
                      private function handleResult(evt:ResultEvent):void
                      {
                          dg1.dataProvider = evt.result.player;
                      }
                  ]]>
                  </mx:Script>
                  
                  <mx:Label x="79" y="89" text="username"/>
                  <mx:Label x="79" y="128" text="rolnumber"/>
                  <mx:TextInput id="nm" x="153" y="87"/>
                  <mx:TextInput x="153" y="126" id="roll"/>
                  <mx:Button x="127" y="191" label="submit" id="sub" click="send_data()"/>
                  
                  <mx:HTTPService id="userRequest" result="handleResult(event)" url="http://localhost/connect.php" useProxy="false" method="POST">
                      <mx:request xmlns="">
                          <username>{nm.text}</username><rollnumber>{roll.text}</rollnumber>
                      </mx:request>
                  </mx:HTTPService>
                  
                  <mx:DataGrid id="dg1" x="69" y="261">
                      <mx:columns>
                          <mx:DataGridColumn headerText="Name" dataField="name"/>
                          <mx:DataGridColumn headerText="Roll" dataField="roll"/>
                      </mx:columns>
                  </mx:DataGrid>
              
              </mx:Application>
              

               

              PHP Code:

               

              <?php
              
                  mysql_connect("localhost", "root", "") or die("cannot connect");
                  mysql_select_db("std_db") or die("cannot select database");
              
                  if ($_POST["username"])
                  {
                      $name = mysql_real_escape_string($_POST["username"]);
                      $roll = mysql_real_escape_string($_POST["rollnumber"]);
              
                      $query = "INSERT INTO student (name , roll) VALUES ('$name', '$roll')";
                      
                      if(mysql_query($query))
                      {
                          echo "sucessfully inserted";
                      }
                      else
                      {
                          echo" error occured";
                      }
                  }
                  
                  $query="SELECT * FROM student";
                  $result = mysql_query($query);
                  
                  $num_rows = mysql_numrows($result);
                      
                  $i=0;
                  
                  while ($i < $num_rows) 
                  {
                      $getName = mysql_result($result,$i,"name");
                      $getRoll = mysql_result($result,$i,"roll");
              
                      print "<player><name>".$getName."</name><roll>".$getRoll."</roll></player>";
                      $i++;
                  }
                  
              ?>