6 Replies Latest reply: Feb 24, 2013 8:04 AM by sinious RSS

    Why do i get "Null" in mysql database?

    lottis789

      Hi

       

      Im trying to update a flash movie quiz that records names and question answers which i didnt write. The quiz works except it replaces the name with null. All the other records work fine. I have deleted out the res2 - res12 just to shorten it up, they all work well. Thanks in advance for any light you can shed on this

       

      From Name input page of the quiz. participname is the text field input

       

      stop();

      btn1.addEventListener(MouseEvent.MOUSE_UP,function():void {

      var Name:String=participname.text.toString();

      gotoAndPlay(3);

      }               

      );

       

       

      From the last page of the quiz

       

      sendData();

              function sendData(){

                 

                      var messages:URLRequest = new URLRequest("./insertresult.php")

                      messages.method = URLRequestMethod.POST

                      var posts:URLVariables = new URLVariables()

                      posts.Name = Name

                      posts.DateCurrent = dtFormatted

                      posts.Res01 = res01

                                                      

                      messages.data = posts

                      trace(posts);

                     

                      var loader:URLLoader = new URLLoader()

                      loader.dataFormat = URLLoaderDataFormat.TEXT

                     // loader.addEventListener(Event.COMPLETE, dataOnLoad)

                      loader.load(messages)

                      trace(messages);

       

      From the insertresult.php

       

      <?php

            //Capture data from $_POST array

          $name = $_POST['Name'];

          $date = $_POST['DateCurrent'];

          $res1 = $_POST['Res01'];

       

          

      /* if(!$name )

      {

        echo "no input using default values <br>";

        $name = 'deleteme';

       

       

      }*/

       

       

            $connect = mysql_connect("mydatabase");

            mysql_select_db ("PAlogindatabase", $connect);

          

            $result = mysql_query("INSERT into rhymeoddity1 (name,date,res1) values ('$name','$date','$res1')");

            if($result) echo "writing=Ok&";

            else echo "writing=Error";

      ?>

        • 1. Re: Why do i get "Null" in mysql database?
          moccamaximum Community Member

          var Name:String

           

          You should never ever (in any programming language) use words that are reserved for your own variables.

          If you must use a var like "name" give it aleading underscore or dollarsign to make it water-proof for the compiler.

          • 2. Re: Why do i get "Null" in mysql database?
            sinious MVP

            Semicolons can really save the compiler a lot of trouble figuring out where a statement ends. While it won't give you an error it can confuse the compiler. Try adding in semicolons at the end of every statement and see if your issue helps.

             

            What mocca said is useful also, to not use reserved property names are vars such as the var 'name'. However you did use 'Name' with a capital N so this does not conflict. You also used 'loader' and there is a display class called 'Loader' with a capital L so again you slipped by a small margin. This isn't a good practice. Make it 'myLoader' or 'dataLoader' or anything but a built in class or property name. It can easily be mistaken or omitted and you can get yourself into trouble. Be careful on the reserved word usage.

            • 3. Re: Why do i get "Null" in mysql database?
              lottis789 Community Member

              Ok i tried renaming the "Name" with "myname" but it hasnt helped I also changed "loader" to "myloader" but again no change

               

              Im not shore were to put in the semicolons ??? Ive tried them in a couple of places but it either bugs out or makes no change

               

              Thanks for the help guys

              • 4. Re: Why do i get "Null" in mysql database?
                lottis789 Community Member

                After another couple of hours of poking ithink ive found that the problem that the var myname isnt getting set correctly.

                 

                var myname:String=participname.text.toString();

                 

                Ive put this into the second page and reads "NULL"

                 

                mytextbox.text = String (participname)

                 

                Cheers Rob

                • 5. Re: Why do i get "Null" in mysql database?
                  lottis789 Community Member

                  I sorted out the problem, i believe that this bit of the code was hiding away the var myname

                   

                  stop();

                  btn1.addEventListener(MouseEvent.MOUSE_UP,function():void {

                  var myname:String=participname.text.toString();

                  gotoAndPlay(3);

                  }               

                  );

                   

                  i have replaced this with this and it is working fine now.

                   

                  stop();

                  var myname:String = "";

                  btn1.addEventListener(MouseEvent.MOUSE_UP,function():void {

                  myname = participname.text;

                  gotoAndPlay(3);

                  }

                  );

                   

                  If someone can explain what the difference is so i can understand

                  • 6. Re: Why do i get "Null" in mysql database?
                    sinious MVP

                    It's a difference of "scope". If you go in a timeline script and init the var outside of a function it will be a "global" var, available for shared use inside functions, like this:

                     

                    var myname:String = "Bob";

                     

                    function myFuncA():void

                    {

                         trace("I can use myname: " + myname); // traces Bob

                    }

                     

                    function myFuncB():void

                    {

                         trace("I can use it too: " + myname); // traces Bob

                    }

                     

                    myFuncA();

                    myFuncB();

                     

                    However, if you make the variable inside a function, it will only exist inside that function. By 'make', I mean use the keyword 'var'. Consider this example where I try to change the var myname:

                     

                    var myname:String = "Bob";

                     

                    function myFuncA():void

                    {

                         // use 'var' and set myname

                         var myname:String = "Sam";

                         trace("I can use myname: " + myname); // traces Sam

                    }

                     

                    function myFuncB():void

                    {

                         trace("I can use it too: " + myname); // traces Bob, not Sam

                    }

                     

                    myFuncA();

                    myFuncB();

                     

                    Notice the second function when used will trace "Bob" even though myFuncA traced "Sam". The reason why is when I used the keyword 'var' inside the function myFuncA I made a brand new variable 'scoped' to the function myFuncA. Once that function ends, the variable is destroyed.

                     

                    Now consider this, where I don't use var:

                     

                    var myname:String = "Bob";

                     

                    function myFuncA():void

                    {

                         // not using 'var' so I'm accessing the global var itself

                         myname = "Sam";

                         trace("I can use myname: " + myname); // traces Sam

                    }

                     

                    function myFuncB():void

                    {

                         trace("I can use it too: " + myname); // traces Sam

                    }

                     

                    myFuncA();

                    myFuncB();

                     

                    Because I didn't re-init the var and used a variable that's global scope directly inside the function, I changed the global value successfully. Now both functions will trace "Sam".

                     

                    Here's more info from Adobe on variable scope:

                    http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9 b90204-7f9d.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7f8c