1 Reply Latest reply on Mar 28, 2010 3:50 PM by Matt Le Fevre

    Why can't I add some data to my DB?

    alcaponis

      The problem appears when i try add some data to phpMyAdmin DB.  Alert.show shows me the line from php file saying that the query is not completed : "echo "<status>Sukurti nepavyko</status>";

      Please help me.

      This is all code from app, php and database:

       

      app

       

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

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" verticalAlign="middle" horizontalAlign="center"

      applicationComplete="klaseinit(), init(), tipasinit()">


          <mx:Script>

         

          <![CDATA[

          import mx.controls.Text;

          import mx.events.ResizeEvent;

              import mx.rpc.events.ResultEvent;

              import mx.controls.Alert;

              import mx.controls.DateField;

              import mx.events.DateChooserEvent;

              import mx.collections.ArrayCollection;

             


              [Bindable]

              public var newUsername:String;

              [Bindable]

              public var newPassword:String;

              [Bindable]

              public var newPassword2:String;

              [Bindable]

              public var newVardas:String;

              [Bindable]

              public var newPavarde:String;

              [Bindable]

              public var newKlase:String;

              [Bindable]

              public var newTipas:int = new int();

              [Bindable]

              public var newTel:String;

              [Bindable]

              public var newAdresas:String;

              [Bindable]

              public var newElpastas:String;

              [Bindable]

              public var newGimimoData:Date = new Date();

              [Bindable]

              public var newGimimoData2:String;

              [Bindable]

              public var dataDump:String = "dataDump";

             

             

              private function klaseinit():void{

              klaseServ.send();

              }

             

              private function tipasinit():void{

              tipasServ.send();

              }


             private function dateChooser_init():void {

                      newGimimoDataTxt.dayNames=['Sekm', 'Pirm', 'Ant', 'Tre', 'Ket', 'Penk', 'Sest'];

                      newGimimoDataTxt.monthNames=['Sausis', 'Vasaris', 'Kovas', 'Balandis', 'Gegužė', 'Birželis', 'Liepa', 'Rugpjūtis', 'Rugsėjis', 'Spalis', 'Lapkritis', 'Gruodis'];

                      newGimimoDataTxt.firstDayOfWeek = 1;

             }

                 

             private function klaseResult(evt:ResultEvent):void{

                  newKlaseTxt.dataProvider = evt.result.status;

             }

            

             private function tipasResult(evt:ResultEvent):void{

                  newTipasTxt.dataProvider = evt.result.status;

             }

            

             private function tipasChange():void{

             if (newTipasTxt.selectedItem == "Mokinys" || newTipasTxt.selectedIndex == 3)

             {

             newKlaseTxt.enabled = true;

             }

             else

             {

             newKlaseTxt.enabled = false;

             }

             }


              private function addNewUser():void{

              if (newUserTxt.text == "" || newPassTxt.text == "" || newPass2Txt.text == "" || newVardasTxt.text == "" || newPavardeTxt.text == "" || newKlaseTxt.text == "" || newGimimoDataTxt.text == "")

              {

              Alert.show("* pažymėti laukai yra būtini!", "Tuščias laukas");

              }

              else

              {

              if (newPassTxt.text != newPass2Txt.text){

              Alert.show("Klaidingai pakartojote antrajį slaptažodį!", "Klaidingai pakartojote");

              newPassTxt.text = "";

              newPass2Txt.text = "";

              }

              else

              {

              newUsername = newUserTxt.text;

      newPassword = newPassTxt.text;

      newVardas = newVardasTxt.text;

      newPavarde = newPavardeTxt.text;

      newKlase = newKlaseTxt.selectedItem.toString();

      newGimimoData = DateField.stringToDate(newGimimoDataTxt.text,"YYYY-MM-DD");

      newTipas = newTipasTxt.selectedIndex.valueOf();

      newElpastas = newElpastasTxt.text;

      newTel = newTelTxt.text;

      newAdresas = newAdresasTxt.text;

       

      newUserTxt.text = "";

      newPassTxt.text = "";

      newPass2Txt.text = "";

      newVardasTxt.text = "";

      newPavardeTxt.text = "";

      newGimimoDataTxt.text = "";

      newElpastasTxt.text = "";

      newTelTxt.text = "";

      newAdresasTxt.text = "";

       

      newUser.send();

              }

             

              }

             }

             

             private function newUserResult(evt:ResultEvent):void{

              Alert.show(evt.result.status, "Result");

              }

             

             

             private function init():void{

              getUsers.send();

              }

             

             

              private function getUsersResult(evt:ResultEvent):void{

              userDG.dataProvider = evt.result.user;

              }



          ]]>

         

          </mx:Script>

         


      <mx:HTTPService id="newUser" url="php\newUser.php" method="POST" result="newUserResult(event)">

          <mx:request xmlns="">

              <newuser>{newUsername}</newuser>

              <newpassword>{newPassword}</newpassword>

      <newvardas>{newVardas}</newvardas>

      <newpavarde>{newPavarde}</newpavarde>

      <newklase>{newKlase}</newklase>

      <newgimimoData>{newGimimoData}</newgimimoData>

          </mx:request>

          </mx:HTTPService>

         

          <mx:HTTPService id="klaseServ" url="php\klase.php" method="POST" result="klaseResult(event)">

          <mx:request xmlns="">

              <data>{dataDump}</data>

          </mx:request>

          </mx:HTTPService>

         

          <mx:HTTPService id="tipasServ" url="php\tipas.php" method="POST" result="tipasResult(event)">

          <mx:request xmlns="">

              <data>{dataDump}</data>

          </mx:request>

          </mx:HTTPService>

         

          <mx:HTTPService id="getUsers" url="php\getUsers.php" method="POST" result="getUsersResult(event)">

          <mx:request xmlns="">

              <data>{dataDump}</data>

          </mx:request>

          </mx:HTTPService>


      <mx:VBox>

      <mx:ApplicationControlBar dock="false">

      <mx:Label text="MENIU" color="blue"/>

      <mx:MenuBar id="myMenuBar" labelField="@label">

                  <mx:XMLList>

                      <menuitem label="MenuItem A" >

                          <menuitem label="SubMenuItem A-1" type="check"/>

                          <menuitem label="SubMenuItem A-2" type="check"/>

                      </menuitem>

                      <menuitem label="MenuItem B"/>

                      <menuitem label="MenuItem C"/>

                      <menuitem label="MenuItem D" >

                          <menuitem label="SubMenuItem D-1" type="radio" groupName="one"/>

                          <menuitem label="SubMenuItem D-2" type="radio" groupName="one"/>

                          <menuitem label="SubMenuItem D-3" type="radio" groupName="one"/>

                      </menuitem>

                  </mx:XMLList>

              </mx:MenuBar>

      <mx:LinkBar dataProvider="{['Facebook','Chat','Friends']}" />

      <mx:rollOverEffect>

      <mx:Glow blurXTo="5" blurYTo="5" color="white" />

      </mx:rollOverEffect>

      </mx:ApplicationControlBar>


      <mx:HBox top="10" horizontalGap="10" left="10" right="10">

      <mx:Panel title="Naujas Vartotojas" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10" layout="vertical" horizontalAlign="center">

      <mx:HBox horizontalGap="10">

         <mx:VBox verticalGap="10" horizontalAlign="right">

           <mx:Label text="*Tipas"/>

            <mx:Label text="*Klasė"/>

            <mx:Label text="*Slapyvardis"/>

            <mx:Label text="*Slaptažodis"/>

            <mx:Label text="*Pakartokite slaptažodį"/>

            <mx:Label text="*Vardas"/>

            <mx:Label text="*Pavardė"/>

            <mx:Label text="Gimimo data"/>

            <mx:Label text="E.Paštas"/>

            <mx:Label text="Telefonas"/>

            <mx:Label text="Adresas"/>

         </mx:VBox>

         <mx:VBox>

           <mx:ComboBox id="newTipasTxt" change="tipasChange()"/>

            <mx:ComboBox id="newKlaseTxt"/>

            <mx:TextInput id="newUserTxt"/>

            <mx:TextInput id="newPassTxt"/>

            <mx:TextInput id="newPass2Txt"/>

            <mx:TextInput id="newVardasTxt"/>

            <mx:TextInput id="newPavardeTxt"/>

            <mx:DateField id="newGimimoDataTxt"

            editable="true"

                  formatString="YYYY-MM-DD"

            width="160" height="22"

            showToday="false"

            displayedMonth="8"

            displayedYear="1999"

            firstDayOfWeek="1"

            creationComplete="dateChooser_init();"

            yearNavigationEnabled="true">

            </mx:DateField>

            <mx:TextInput id="newElpastasTxt"/>

            <mx:TextInput id="newTelTxt"/>

            <mx:TextInput id="newAdresasTxt"/>

         </mx:VBox>

      </mx:HBox>

         <mx:Button label="SUKURTI" click="addNewUser();"/>

      </mx:Panel>

       

       

          <mx:Panel title="Visi Vartotojai" height="390">

             

                  <mx:DataGrid id="userDG" height="350" horizontalScrollPolicy="on" verticalScrollPolicy="on">

                      <mx:columns>

                      <mx:DataGridColumn headerText="Klasė" dataField="klase" width="40" textAlign="center"/>

                      <mx:DataGridColumn headerText="Tipas" dataField="tipas" width="40" textAlign="center"/>

                      <mx:DataGridColumn headerText="Pavardė" dataField="pavarde"/>

                      <mx:DataGridColumn headerText="Vardas" dataField="vardas"/>

                          <mx:DataGridColumn headerText="Slapyvardis" dataField="username"/>

                          <mx:DataGridColumn headerText="Slaptažodis" dataField="password"/>

                          <mx:DataGridColumn headerText="Gimimo data" dataField="gimimodata"/>

                          <mx:DataGridColumn headerText="E.Paštas" dataField="elpastas"/>

                          <mx:DataGridColumn headerText="Tel" dataField="tel"/>

                          <mx:DataGridColumn headerText="Adresas" dataField="adresas"/>

                      </mx:columns>

                  </mx:DataGrid>

             

          </mx:Panel>

      </mx:HBox>

      </mx:VBox>

      </mx:Application>

      php file
      <?PHP
      $hostname_conn = "localhost";
          $username_conn = "root";
      $password_conn = "";

          $conn = mysql_connect($hostname_conn, $username_conn, $password_conn);

      mysql_select_db("socDB");
      //$values = $_POST['id'];
      //mysql_real_escape_string POST'ed data for security purposes
          $newuser1 = $_POST["newuser"];
          $newpassword1 = $_POST["newpassword"];
      $newvardas1 = $_POST["newvardas"];
      $newpavarde1 = $_POST["newpavarde"];
      $newklase1 = $_POST["newklase"];
      $newgimimoData1 = $_POST["newgimimoData"];
      $newtipas1 = $_POST["newtipas"];
      $newelpastas1 = $_POST["newelpastas"];
      $newtel1 = $_POST["newtel"];
      $newadresas1 = $_POST["newadresas"];
      $query = "SELECT * FROM vartotojas WHERE USERNAME = '$newuser'";

      $result = mysql_query($query);

      $logged = mysql_num_rows($result);

      if ($logged == 1)
      {
      $log = false;
      }
      else
      {
      $log = true;
      }
      if ($log == true)
      {
      $sql = "INSERT INTO vartotojas (USERNAME, PASSWORD. VARDAS, PAVARDE, KLASE, GIMIMODATA, TIPAS, ELPASTAS, TEL, ADRESAS) VALUES('$newuser1','$newpassword1','$newvardas1','$newpavarde1', '$newklase1','$newgimimoData1','$newtipas','newtipas1','$newtel1','$newadresas1)";
      if (!mysql_query($sql, $conn))
      {
      echo "<status>Sukurti nepavyko</status>";
      }
      else
      {
      echo "<status>Naujas vartotojas sukurtas!</status>";
      }
      }
      else
      {
      echo "<status>Šis vartotojo vardas yra užimtas, bandykite kita!</status>";
      }
      ?>
      phpMyAdmin structure:
      myadmin.JPG
      Please help!
        • 1. Re: Why can't I add some data to my DB?
          Matt Le Fevre Level 4
          $sql = "INSERT INTO vartotojas (USERNAME, PASSWORD. VARDAS, PAVARDE, KLASE, GIMIMODATA, TIPAS, ELPASTAS, TEL, ADRESAS) VALUES('$newuser1','$newpassword1','$newvardas1','$newpavarde1', '$newklase1','$newgimimoData1','$newtipas','newtipas1','$newtel1','$newadresas1')";

           

          you appear to have missed a quotation mark right at the end of this statement, i've highlighted it in red.

           

          a brief check over, you seem to be doing everything correctly, my bet would be that you've made a simple mistake in your syntax like i mentioned above, you'll just have to check through it thoroughly.

           

           

          if it still doesn't work, i'd suggest cutting it down in size, try to add one field and get that working. Once it does, build it up from there testing each time and see when it goes wrong.

           

           

          i think i've already mentioned to you that i have an example app here that adds information to a database:

           

          http://www.mattlefevre.com/viewExample.php?tut=flexPHP&proj=Simple%20Login%20Application

           

          i'd recommend going back to that if you still can't get it to work, as i know that's working fine