2 Replies Latest reply on Dec 26, 2011 7:23 AM by gabbyrub

    Flex Mobile - Php INSERT

    SaidT

      Hello everybody, im a novice on Developpement using Flex.

      im working on a project which will be runned on BlackBerry Tablet (PlayBook), im using FlashBuilder Burrito + AIR SDK

      my project is a CRUD application, so i follow this tutorial http://www.adobe.com/devnet/flex/testdrivemobile/articles/mtd_1_1.html and it works

      now i try to do the same things on my DataBase, i can retrieve data but i can't Insert or Delete ...

       

      this is the code of my application :

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:View 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:valueObjects="valueObjects.*"
                xmlns:affiliesservice="services.affiliesservice.*"
                title="Accueil">
      
           <fx:Script>
                <![CDATA[
                     import mx.rpc.events.ResultEvent;
                     protected function saveBtn_clickHandler(event:MouseEvent):void
                     {
                          aff.longitude=11111.1;
                          aff.lattitude=11111.3;
                          createAffilies(aff);
                     }
                     
                     protected function createAffilies(item:Affilies):void
                     {
                          createAffiliesResult.token = affiliesService.createAffilies(item);
                     }
                     
      
                     protected function createAffiliesResult_resultHandler(event:ResultEvent):void
                     {
                          navigator.popView();
                          navigator.pushView(ajoutValides);
                     }
      
                ]]>
           </fx:Script>
      
           <fx:Declarations>
                <valueObjects:Affilies id="aff" />
                <s:CallResponder id="createAffiliesResult" result="createAffiliesResult_resultHandler(event)"/>
                <affiliesservice:AffiliesService id="affiliesService"/>
           </fx:Declarations>
           <s:Scroller left="0" right="0" top="0" bottom="103">
                <s:VGroup x="38" y="110" width="100%" height="100%" gap="15" paddingBottom="15"
                            paddingLeft="15" paddingRight="15" paddingTop="15">
                     <s:Label text="Nom"/>
                     <s:TextInput width="100%" text="@{aff.nom}"/>
                     <mx:Spacer height="5"/>
                     <s:Label text="Adresse"/>
                     <s:TextInput width="100%" text="@{aff.adr}"/>
                     <mx:Spacer height="5"/>
                     <s:Label text="Telephone"/>
                     <s:TextInput width="100%" text="@{aff.tel}"/>
                     <mx:Spacer height="5"/>
                     <s:Label text="Url"/>
                     <s:TextInput width="100%" text="@{aff.lien}"/>
                     <mx:Spacer height="5"/>
                </s:VGroup>
           </s:Scroller>
           <s:Button id="saveBtn" left="10" bottom="10" width="45%" label="Save"
                       click="saveBtn_clickHandler(event)"/>
           <s:Button id="cancelBtn" right="17" bottom="10" width="45%" label="Cancel"/>
      </s:View>
      
       
      

       

       

      and for the service i generate it automatically and this is the code :

       

       

      <?php
      
      /**
       *  README for sample service
       *
       *  This generated sample service contains functions that illustrate typical service operations.
       *  Use these functions as a starting point for creating your own service implementation. Modify the 
       *  function signatures, references to the database, and implementation according to your needs. 
       *  Delete the functions that you do not use.
       *
       *  Save your changes and return to Flash Builder. In Flash Builder Data/Services View, refresh 
       *  the service. Then drag service operations onto user interface components in Design View. For 
       *  example, drag the getAllItems() operation onto a DataGrid.
       *  
       *  This code is for prototyping only.
       *  
       *  Authenticate the user prior to allowing them to call these methods. You can find more 
       *  information at http://www.adobe.com/go/flex_security
       *
       */
      class AffiliesService {
      
           var $username = "root";
           var $password = "root";
           var $server = "localhost";
           var $port = "3306";
           var $databasename = "expressway";
           var $tablename = "affilies";
      
           var $connection;
      
           /**
            * The constructor initializes the connection to database. Everytime a request is 
            * received by Zend AMF, an instance of the service class is created and then the
            * requested method is invoked.
            */
           public function __construct() {
                  $this->connection = mysqli_connect(
                                                $this->server,  
                                                $this->username,  
                                                $this->password, 
                                                $this->databasename,
                                                $this->port
                                           );
      
                $this->throwExceptionOnError($this->connection);
           }
      
           /**
            * Returns all the rows from the table.
            *
            * Add authroization or any logical checks for secure access to your data 
            *
            * @return array
            */
           public function getAllAffilies() {
      
                $stmt = mysqli_prepare($this->connection, "SELECT * FROM affilies");          
                $this->throwExceptionOnError();
                
                mysqli_stmt_execute($stmt);
                $this->throwExceptionOnError();
                
                $rows = array();
                
                mysqli_stmt_bind_result($stmt, $row->id, $row->nom, $row->adr, $row->tel, $row->longitude, $row->lattitude, $row->lien);
                
               while (mysqli_stmt_fetch($stmt)) {
                 $rows[] = $row;
                 $row = new stdClass();
                 mysqli_stmt_bind_result($stmt, $row->id, $row->nom, $row->adr, $row->tel, $row->longitude, $row->lattitude, $row->lien);
               }
                
                mysqli_stmt_free_result($stmt);
               mysqli_close($this->connection);
           
               return $rows;
           }
      
           /**
            * Returns the item corresponding to the value specified for the primary key.
            *
            * Add authorization or any logical checks for secure access to your data 
            *
            * 
            * @return stdClass
            */
           public function getAffiliesByID($itemID) {
                
                $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where id=?");
                $this->throwExceptionOnError();
                
                mysqli_stmt_bind_param($stmt, 'i', $itemID);          
                $this->throwExceptionOnError();
                
                mysqli_stmt_execute($stmt);
                $this->throwExceptionOnError();
                
                mysqli_stmt_bind_result($stmt, $row->id, $row->nom, $row->adr, $row->tel, $row->longitude, $row->lattitude, $row->lien);
                
                if(mysqli_stmt_fetch($stmt)) {
                 return $row;
                } else {
                 return null;
                }
           }
      
           /**
            * Returns the item corresponding to the value specified for the primary key.
            *
            * Add authorization or any logical checks for secure access to your data 
            *
            * 
            * @return stdClass
            */
           public function createAffilies($item) {
      
                $stmt = mysqli_prepare($this->connection, "INSERT INTO affilies (nom, adr, tel, longitude, lattitude, lien) VALUES (?, ?, ?, ?, ?, ?)");
                $this->throwExceptionOnError();
      
                mysqli_stmt_bind_param($stmt, 'sssdds', $item->nom, $item->adr, $item->tel, $item->longitude, $item->lattitude, $item->lien);
                $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;
           }
      
           /**
            * Updates the passed item in the table.
            *
            * Add authorization or any logical checks for secure access to your data 
            *
            * @param stdClass $item
            * @return void
            */
           public function updateAffilies($item) {
           
                $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET nom=?, adr=?, tel=?, longitude=?, lattitude=?, lien=? WHERE id=?");          
                $this->throwExceptionOnError();
                
                mysqli_stmt_bind_param($stmt, 'sssddsi', $item->nom, $item->adr, $item->tel, $item->longitude, $item->lattitude, $item->lien, $item->id);          
                $this->throwExceptionOnError();
      
                mysqli_stmt_execute($stmt);          
                $this->throwExceptionOnError();
                
                mysqli_stmt_free_result($stmt);          
                mysqli_close($this->connection);
           }
      
           /**
            * Deletes the item corresponding to the passed primary key value from 
            * the table.
            *
            * Add authorization or any logical checks for secure access to your data 
            *
            * 
            * @return void
            */
           public function deleteAffilies($itemID) {
                          
                $stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE id = ?");
                $this->throwExceptionOnError();
                
                mysqli_stmt_bind_param($stmt, 'i', $itemID);
                mysqli_stmt_execute($stmt);
                $this->throwExceptionOnError();
                
                mysqli_stmt_free_result($stmt);          
                mysqli_close($this->connection);
           }
      
      
           /**
            * Returns the number of rows in the table.
            *
            * Add authorization or any logical checks for secure access to your data 
            *
            * 
            */
           public function count() {
                $stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
                $this->throwExceptionOnError();
      
                mysqli_stmt_execute($stmt);
                $this->throwExceptionOnError();
                
                mysqli_stmt_bind_result($stmt, $rec_count);
                $this->throwExceptionOnError();
                
                mysqli_stmt_fetch($stmt);
                $this->throwExceptionOnError();
                
                mysqli_stmt_free_result($stmt);
                mysqli_close($this->connection);
                
                return $rec_count;
           }
      
      
           /**
            * Returns $numItems rows starting from the $startIndex row from the 
            * table.
            *
            * Add authorization or any logical checks for secure access to your data 
            *
            * 
            * 
            * @return array
            */
           public function getAffilies_paged($startIndex, $numItems) {
                
                $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
                $this->throwExceptionOnError();
                
                mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
                mysqli_stmt_execute($stmt);
                $this->throwExceptionOnError();
                
                $rows = array();
                
                mysqli_stmt_bind_result($stmt, $row->id, $row->nom, $row->adr, $row->tel, $row->longitude, $row->lattitude, $row->lien);
                
               while (mysqli_stmt_fetch($stmt)) {
                 $rows[] = $row;
                 $row = new stdClass();
                 mysqli_stmt_bind_result($stmt, $row->id, $row->nom, $row->adr, $row->tel, $row->longitude, $row->lattitude, $row->lien);
               }
                
                mysqli_stmt_free_result($stmt);          
                mysqli_close($this->connection);
                
                return $rows;
           }
           
           
           /**
            * Utility function to throw an exception if an error occurs 
            * while running a mysql command.
            */
           private function throwExceptionOnError($link = null) {
                if($link == null) {
                     $link = $this->connection;
                }
                if(mysqli_error($link)) {
                     $msg = mysqli_errno($link) . ": " . mysqli_error($link);
                     throw new Exception('MySQL Error - '. $msg);
                }          
           }
      }
      
      ?>
      
       
      

       

       

      and i can't even find an error !! could anyone help me or even give me a solution to read the error report.

      thx.