5 Replies Latest reply: Jul 23, 2014 7:58 AM by vagz RSS

    Unable to create records in database using PHP Data Service

    Rybicon

      Hello, I've been stuck on this for a few days and search up and down for this on the net, no response I've found has worked, so I come to you...

       

      Here are the steps I've taken, I think it's pretty standard

       

      1. I have a macbook pro running osx 10.7.3

      2. I installed MAMP all default (I've acually reinstalled this because someone suggested this might fix it)

      3. Thru phpMyAdmin I created a database called my_test

      4. In that database I created a table, this is the export of that table: (I've also tried this with InnoDB which is the default)

       

      CREATE TABLE `customer` (

        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

        `name` varchar(50) NOT NULL,

        `email` varchar(150) NOT NULL,

        PRIMARY KEY (`id`),

        UNIQUE KEY `id` (`id`)

      ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

       

      5. I created a new Flx Project (Running Flash Builder 4.5.1 Premium)

           Project Name: PHPTest

           Application Type: Desktop (Although I've done the same thing with Web and got the same results)

           >> Next

           Application Server Type: PHP

           Web Root: /Applications/MAMP/htdocs/

           URL Root: http://localhost:8888/

           Clicked 'Validate Configuration' and that worked

           Output Folder: /Applications/MAMP/htdocs/PHPService (default by Flash Builder)

           >> Finished

      6. On the Data/Services tab on the bottom I click 'Connect to Data/Service...'

           Select 'PHP'

           >> Next

           Select 'Click here to generate a sample'

           Select 'Generate from database' radio button

           Username: root

           Password: root (default for MAMP)

           Host name: localhost

           Server port: 8889 (default for MAMP MySQL port, the HTTP port default is 8888 which in both cases seem to work but every video I've seen that uses MAMP on youtube uses 8889)

           Database: my_test

           Click 'Test Connection' (works)

           Table: customer

           Primary Key: id (this field is greyed out and Flash Builder selects 'id' which it figures out from the SQL table)

           >> Click OK

      7. Then if I don't have the Zend Framework folder in my /Applications/MAMP/htdocs/ folder it tells me its going to create that and I say alright. Then Flash Builder says stuff about how this is really only for testing and not production server ready and I say alright.

      8. Then it takes me back to the Form from Step 6 when I get the chance to select 'Click here to generate a sample'

           These fields are now filled with this data automaticly:

           PHP Class: /Applications/MAMP/htdocs/PHPTest/services/CustomerService.php

           Service name: CustomerService

           Service package: services.customerservice

           Data type package: valueObjects

           >> Click Next (shows all the functions that will be now availible)

           >> Click Finished (End of the forms and it opens Dreamweaver to the php file it created CustomerService.php which I have no need to edit so I close that down)

      9. Back in Flash Builder I switch to design view and drag a datagrid onto the big white area in the middle, whatever that is called.

      10. Then below in Data/Services tab on the bottom I click drag the function 'GetAllCustomer' on top of the datagrid.

           I say yes to rebound and click ok and the view of the datagrid is updated with the colums from the 'customer' table in mySQL.

       

      (Now let me say that when I hit save and compile this, if I actually had records in this table that I insert thru phpMyAdmin, this does show in the datagrid. So for the whole CRUD thing I am able to get the R which is Read)

       

      11. Now going back to the design view in Flash Builder I will create a form to create records in the table... (I guess this isn't really a step)

      12. In the Data/Service tab on the bottom I select 'createCustomer' function and then there is an icon called 'Generate Form' that looks like a white piece of paper with a gear on the bottom of it.

      13. This opens up a new form and since I don't have a crazy bunch of fields in my table I just click Finished (If you click next you can specify which fields you want to exclude from the form but this time I don't need to)

      14. This actually creates 2 forms if you look at the code, the second just shows the return type from when you click 'CreateCustomer' button on the first form. Because they overlap in design view I drag the form out of the way so you can see the input form, the return form, and the datagrid

      15. Then I save and compile...

      (Also if your actually reading this I didn't remove the id form field, I get the same result either way, but if you do remove the form field in the code/design you also have to update the button function to not deal with the id before it gets sent off to the php page since in this case the MySQL table is set to auto_increment the id, sorry this doesn't make much sence but this little area doesn't matter much either way)

      16. Now fill in whatever data you want for name and email, try differnt numbers in the id field like 0, nothing, 1, 1000 and click 'CreateCustomer'

      (For me nothing happens, no return is put in the return field, no error pops up and the datagrid is not updated with the new record, also going over to phpMyAdmin and checking out the table browse doesn't show any changes either, I know the button is calling the function because if I add a state change in that function it changes, it would seem that the line:

      createCustomerResult.token = customerService.createCustomer(customer2); doesn't do anything)

       

      So any idea what is wrong here, I'm convenced it's something stupid easy simple I just can't see it.

        • 1. Re: Unable to create records in database using PHP Data Service
          Rybicon Community Member

          In case you need more info, here is a code dump on a mxml project that has this problem:

           

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

          <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

                                 xmlns:s="library://ns.adobe.com/flex/spark"

                                 xmlns:mx="library://ns.adobe.com/flex/mx"

                                 xmlns:customerservice="services.customerservice.*"

                                 xmlns:valueObjects="valueObjects.*"

                                 currentState="State1">

              <fx:Script>

                  <![CDATA[

                      import mx.controls.Alert;

                      import mx.events.FlexEvent;

                     

                      protected function dataGrid_creationCompleteHandler(event:FlexEvent):void

                      {

                          getAllCustomerResult.token = customerService.getAllCustomer();

                      }

                     

                     

                      protected function button_clickHandler(event:MouseEvent):void

                      {

                          var customer2:Customer = new Customer();

                          customer2.id = parseInt(idTextInput.text);

                          customer2.name = nameTextInput.text;

                          customer2.email = emailTextInput.text;

                         

                          currentState = "Test";

                         

                          createCustomerResult.token = customerService.createCustomer(customer2);

                      }

                     

                  ]]>

              </fx:Script>

              <s:states>

                  <s:State name="State1"/>

                  <s:State name="Test"/>

              </s:states>

              <fx:Declarations>

                  <s:CallResponder id="getAllCustomerResult"/>

                  <customerservice:CustomerService id="customerService"

                                                   fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"

                                                   showBusyCursor="true"/>

                  <valueObjects:Customer id="customer"/>

                  <s:CallResponder id="createCustomerResult"/>

                  <!-- Place non-visual elements (e.g., services, value objects) here -->

              </fx:Declarations>

              <s:DataGrid id="dataGrid" includeIn="State1" x="330" y="10" width="392"

                          creationComplete="dataGrid_creationCompleteHandler(event)" requestedRowCount="4">

                  <s:columns>

                      <s:ArrayList>

                          <s:GridColumn dataField="id" headerText="id"></s:GridColumn>

                          <s:GridColumn dataField="name" headerText="name"></s:GridColumn>

                          <s:GridColumn dataField="email" headerText="email"></s:GridColumn>

                      </s:ArrayList>

                  </s:columns>

                  <s:typicalItem>

                      <fx:Object id="id1" email="email1" name="name1"></fx:Object>

                  </s:typicalItem>

                  <s:AsyncListView list="{getAllCustomerResult.lastResult}"/>

              </s:DataGrid>

              <s:Form includeIn="State1" defaultButton="{button}">

                  <s:FormItem label="Id">

                      <s:TextInput id="idTextInput" text="{customer.id}"/>

                  </s:FormItem>

                  <s:FormItem label="Name">

                      <s:TextInput id="nameTextInput" text="{customer.name}"/>

                  </s:FormItem>

                  <s:FormItem label="Email">

                      <s:TextInput id="emailTextInput" text="{customer.email}"/>

                  </s:FormItem>

                  <s:Button id="button" label="CreateCustomer" click="button_clickHandler(event)"/>

              </s:Form>

              <s:Form includeIn="State1" x="0" y="204">

                  <s:FormItem label="CreateCustomer">

                      <s:TextInput id="createCustomerTextInput" text="{createCustomerResult.lastResult as int}"/>

                  </s:FormItem>

              </s:Form>

          </s:WindowedApplication>

          • 2. Re: Unable to create records in database using PHP Data Service
            Rybicon Community Member

            Alright, I've figured it out after 24 hours of posting my question and  probing every thing I could think of. I've been stuck with this problem for more than a week so I'm going to give myself credit for this one...

             

            userService.commit();

            needed to be added to the end of the button function to force flex to do what seems every tutorial I've ever read online about how to do this doesn't need or mention, not sure why it worked without for them but at this point I don't really care.

            • 3. Re: Unable to create records in database using PHP Data Service
              FM_Flame Community Member

              commit() works only the first time the button is clicked. Clicking the button twice won't create a new database record. Does anyone has a solution to this one ?

              • 4. Re: Unable to create records in database using PHP Data Service
                BBlommers

                Not sure if I can be of any help, since I never use Zend/Mamp/Flash Builder, but...

                 

                The SQL-statement used to create the table says the ID-field is auto-incremented.

                Your code might fail because you are

                a) inserting an ID that the database wants to create himself

                b) inserting an ID that already exists

                • 5. Re: Unable to create records in database using PHP Data Service
                  vagz

                  Thank you very much for your Solution. I've been struggling with this for over a week.

                  You are right; no tutorial, not from lynda.com nor even adobe seemed to mention the commit() method.

                  Since this is a pretty old post I don't know if you'll get a notification about my reply but thanks anyway you kindda saved my day...