6 Replies Latest reply on Oct 26, 2009 6:05 PM by mewk

    Insert problems with Php Data Services

    Desiclimber

      Hi All,

       

      I am using Flex Data wizards to create a simple CRUD application and i am having problems adding data. It works the first time i start my application but subsequent calls to the insert fail.

       

      I am using FB2 Beta with MAMP. The php classes are being generated using the data wizard. i have tried commenting out mysqli_close but still it doesn't work.

       

      public function createDive_events($item) {

       

      $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename ( divedate, divesite, divecost, description, totalDays) VALUES ( ?, ?, ?, ?, ?)");

      $this->throwExceptionOnError();

       

      mysqli_bind_param($stmt, 'ssdsi', $item->divedate, $item->divesite, $item->divecost, $item->description, $item->totalDays);

      $this->throwExceptionOnError();

       

      mysqli_stmt_execute($stmt);

      $this->throwExceptionOnError();

       

      $autoid = mysqli_stmt_insert_id($stmt);

       

      mysqli_stmt_free_result($stmt);

      //mysqli_close($this->connection);

       

      return $autoid;

      }

       

      Any ideas, what i am doing wrong. I have tried using the update and it works only once as well, it seems that the connection to the function is lost after the first call.

       

      thank you,

      Nayan

        • 1. Re: Insert problems with Php Data Services
          mewk Level 3

          Hi Desiclimber,

           

          To begin with I'm not a big fan of mixing oo and procedural styles w/in the same code block (this can trip you up, but I don't think it's causing an error here).

           

          Secondly, I don't understand your use of statements. Prepared statements speed up the process of inserting/updating/selecting multiple queries in a row (think hundreds of insertions), but here there is only a single INSERT. If you intend to do multiple insertions on single call, then createDive_events() should take an array of objects and within the code you need to cycle through them.

           

          Lastly, I don't know how $this->connection is getting initialized -- perhaps that's what's tripping you up? Good luck,

           

          - e

          • 2. Re: Insert problems with Php Data Services
            Desiclimber Level 1

            thank you for your reply,

             

             

            the code that i have posted is what being auto generated by the php data wizard with FB2, the connection is being initialized in the constructor of the class.

             

            public function __construct() {

              $this->connection = mysqli_connect(

              $this->server, 

              $this->username, 

              $this->password,

              $this->databasename,

              $this->port

              );

             

            $this->throwExceptionOnError($this->connection);

            }

             

             

            this code works just fine during the first insert but then it doesn't get executed during the second insert. if i close the flex app and then insert something it works fine.

            • 3. Re: Insert problems with Php Data Services
              mewk Level 3

              Desiclimber,

               

              Sorry, I've never used FB to generate the php classes. I just tried myself and confirm that the create statements only execute once successfully. This should probably be logged to jira as a bug. Will dig into the code and let you know if I find a workaround. Thanks,

               

              - e

              • 4. Re: Insert problems with Php Data Services
                mewk Level 3

                Ok, this is what I got.

                1. create a mysql table (`people`) with columns id (auto increment), first_name, last_name, age
                2. in the data/service panel connect to a php service, generate a php service from the database and the people table
                3. generate a form from the createPeople service call
                4. observe that in source view we have something like this:
                  <fx:Script>
                    <![CDATA[
                         protected function button_clickHandler(event:MouseEvent):void {
                            createPeopleResult.token = peopleService.createPeople(people);
                         }
                  
                    ]]>
                  </fx:Script>
                  
                  <fx:Declarations>
                    <valueObjects:People id="people" 
                                         people_id="{parseInt(people_idTextInput.text)}"
                                         age="{parseInt(ageTextInput.text)}"/>
                    <peopleservice:PeopleService id="peopleService"/>
                    <s:CallResponder id="createPeopleResult"/>
                  </fx:Declarations>
                  

                 

                If you now try to execute the code, you will find that you can only insert one person, where each successive addition silently fails.

                 

                There is nothing really wrong with the php code (well actually since it's auto incrementing on id you should pull all id reference out of the insert statements...) rather there is a problem with the flex people object.

                 

                If you make the following changes the code will work

                protected function button_clickHandler(event:MouseEvent):void {
                  var myPerson:People = new People();
                  myPerson.first_name = people.first_name;
                  myPerson.last_name = people.last_name;
                  myPerson.age = people.age;
                
                  createPeopleResult.token = peopleService.createPeople(myPerson);
                }
                

                 

                - e

                • 5. Re: Insert problems with Php Data Services
                  Desiclimber Level 1

                  cool, thanks a lot that worked. So it seems to me that there is some sort of binding created with the value object in the declaration and that gets executed only once for every operation call or something like that.

                  • 6. Re: Insert problems with Php Data Services
                    mewk Level 3

                    Filed a bug report here: http://bugs.adobe.com/jira/browse/FB-23621

                     

                    There is a two-way binding between the value object and the text fields of the form. The value object is changing, however, the service doesn't seem to want to execute after the first call. Maybe it was designed to avoid duplicate sends and it doesn't realize the argument object has been modified. Dunno, not really sure.

                     

                    - e