1 Reply Latest reply on Sep 26, 2009 12:15 PM by sdothard

    Can't find my error, trying to UPDATE via JSON

    sdothard

      Hello All, I'm brand new to Flex and am looking for a great forum to join. Is this the best forum to share issues or do you guys use other forums also?

       

      I've been going over the "Using Flex, PHP and JSON to Modify a MySQL Database" tutorial. For some strange reason I can't make this tutorial work. I've completed it 5 times and it's worked once. The tutorial is in Flex Builder 2 and I'm using 3, don't think that has anything to do with me not being able to make it work though.

       

      Can you guys help me find my error. I can retreive data just find with the getPHPData function so I didn't add that code. The retreived data is loaded into a Data Grid and there is an "Update" button and the "Email" field is editable. Well, when I make changes to the email field and click the Update button, which fires the sendPHPData() function, nothing happens. I posted the sendPHPData code, the binding that binds the editied results back to the Data Grid  and the HttpService that transmits the data.

       

      Take a look please, can you see why this isn't working.

       

      BUTTON CODE

      <mx:Button x="10" y="259" label="UpdateDatabase" id="butUpdate" click="{sendPHPData()}"/>

      HTTPSERVICE CODE

      <mx:HTTPService id="sendData" url="\flexJSON-debug\mainPHPfile.php"
        useProxy="false" method="GET" resultFormat="text"
        result="updatedPHPDataResult(event)">
      </mx:HTTPService>

       

      ACTIONSCRIPT SENDPHPDATA FUNCTION

      private function sendPHPData():void
      {
      var objSend:Object = new Object();
      var dataString:String = JSON.encode(dataArray.toArray());
      dataString = escape(dataString);
      objSend.setTutorials = "true";
      objSend.jsonSendData = dataString;
      sendData.send(objSend);
                               
      lblStatus.text = "hello"; //This is used just to test that the function is getting called, and it is.
                               
      }

       

      DATA BINDING FOR DATA GRID

       

      <mx:Binding source="dgData.dataProvider as ArrayCollection" destination="dataArray"/>

       

        • 1. Re: Can't find my error, trying to UPDATE via JSON
          sdothard Level 1

          Oops, I forgot to post the PHP code, see below.

           

          elseif(isset($_GET['setTutorials']))

           

          {
                      $jsonString = urldecode($_GET['jsonSendData']);
                      $jsonString = str_replace("\\", "", $jsonString);
                      $data = json_decode($jsonString, true);
                     
                      mysql_connect($URL, $USERNAME, $PASSWORD);
                      mysql_select_db($DATABASE) or die('Cannot connect to database.');
                     
                      foreach ($data as $tutorialEntry) {
                        $query = sprintf(
                            'UPDATE employees SET email = "%s" WHERE employeeNumber = "%s"',
                            mysql_real_escape_string($tutorialEntry['email']),
                            mysql_real_escape_string($tutorialEntry['employeeNumber']));
                     
                        $result = mysql_query($query);
                           
                        if(!$result)
                        {
                            mysql_close();
                            echo mysql_error();
                            return;
                        }
                      }
                      mysql_close();
                      echo "database updated";

           

          }