13 Replies Latest reply on Jan 15, 2013 5:26 AM by xmasgame

    AS2 - Problem with inserting score/name to mysql/database

    xmasgame

      Hello,

       

      First of all.. sorry that I havn't put the AS in a box, not sure how to do it on this forum!

      Okey so this is my problem:

      I want to sumbit my score and name into my database.

      This is the script I have tried in Flash.

       

      stop();

       

       

       

      //create loadVars Objects

      playerx = new LoadVars();

      playerRegistered = new LoadVars();

      playerRegistered.onLoad = showResult;

      //create button handler code to call function which sends & loads php

      _root.submitt.onRelease = submitScore;

       

      function submitScore():Void {

                  playerx.name = name_txt.text;

                  playerx.score = score_txt.text;

                  playerx.sendAndLoad("register.php",playerRegistered);

      }

       

       

      This is the PHP script:

       

      <?php

       

      $connect = mysql_connect('x.com.mysql','x','password');

      mysql_select_db('x', $connect);

       

      $sql = 'INSERT INTO high_scores_avoidthecars(name,score) VALUES ("'.$_POST['name'].'","'.$_POST['score'].'")';

       

      ?>

       

      Nothing gets posted to the database, no error etc.. any ideas?

        • 1. Re: AS2 - Problem with inserting score/name to mysql/database
          Ned Murphy Adobe Community Professional & MVP

          I am not sure why you are creating two LoadVars instances.  You should only need the playerx one.  Here's a link to some example code.  See if adjusting yours to match it helps.

           

          http://snipplr.com/view/8878/

          1 person found this helpful
          • 2. Re: AS2 - Problem with inserting score/name to mysql/database
            xmasgame Level 1

            Have made a change, only need one LoadVar actually:

             

             

            stop();

             

             

             

            //create loadVars Objects

            playerx = new LoadVars();

            _root.submitt.onRelease = submitScore;

             

            function submitScore():Void {

                        playerx.name = name_txt.text;

                        playerx.score = score_txt.text;

                        playerx.sendAndLoad("register.php");

            }

             

            I think my problem is like this:

            Connection:

            PHP -> Database = Working

            Flash -> PHP = Doesn't work

            • 3. Re: AS2 - Problem with inserting score/name to mysql/database
              xmasgame Level 1

              Checked the link but I'm a little clueless when it comes to Flash -> php -> database, to be honest..

              I have searched all day long, no good tutorials, no good explainings to be honest.

              I do not know why, but I find this hard to understand.. even if I know how to do this without problems from HTML and PHP

              Hope someone could explain me as I'm running out of places to find it out.

              • 4. Re: AS2 - Problem with inserting score/name to mysql/database
                xmasgame Level 1

                Bump*

                So bored of not getting this.. gahhh

                xmasgame wrote:

                 

                Checked the link but I'm a little clueless when it comes to Flash -> php -> database, to be honest..

                I have searched all day long, no good tutorials, no good explainings to be honest.

                I do not know why, but I find this hard to understand.. even if I know how to do this without problems from HTML and PHP

                Hope someone could explain me as I'm running out of places to find it out.

                • 5. Re: AS2 - Problem with inserting score/name to mysql/database
                  Ned Murphy Adobe Community Professional & MVP

                  If you went thru the link I provided you should have seen where you need to make changes to the code you showed to get the Flash code working.  Here is a link to a working example that reflects the second part of the code of that link that involves sending data (starting at line 28) with your textfields/etc.

                   

                  http://www.nedwebs.com/Flash/register.html

                   

                  The main problem with your AS2 code involves the following line...

                   

                       playerx.sendAndLoad("register.php");

                   

                  it should be...

                   

                       playerx.sendAndLoad("register.php", playerx. "POST");

                   

                   

                  Here is all of the code I used for the example files I made...

                   

                  AS2 CODE

                   

                  playerx = new LoadVars();

                   

                  _root.submitt.onRelease = submitScore;


                  function submitScore():Void {

                   

                              response.text =  "clicked\n";

                   

                              playerx.name = name_txt.text;

                              playerx.score = score_txt.text;

                              playerx.sendAndLoad("register.php", playerx, "POST");

                  }

                   

                  playerx.onLoad = onLoadCallBack;

                   

                  function onLoadCallBack(succes)
                  {
                        response.text += "onLoad Processed\n";


                        if(succes) {
                             response.text += "Success\n";
                             response.text += this.lVar1+"\n";
                             response.text += this.lVar2;
                        } else {
                             response.text = "Loading Error!!";
                        }
                  }

                   

                   

                  PHP CODE

                   

                  <?
                  // get variables
                  $var1 = $_POST['name'];
                  $var2 = $_POST['score'];

                  // send variables
                  echo "&lVar1=$var1 returned&";
                  echo "&lVar2=$var2 returned as well&";
                  ?>

                  • 6. Re: AS2 - Problem with inserting score/name to mysql/database
                    xmasgame Level 1

                    Thanks will check it out. I actually managed to get future last nigth rigth before I went to bed, but my network was to bad to upload it to the servers do couldn't check if I managed to make it works. Will reply here later today. Thanks again!

                    • 7. Re: AS2 - Problem with inserting score/name to mysql/database
                      xmasgame Level 1

                      Just got the chance to upload it and it worked! I had to have highscore table to manage to make it work actually.. anyhow it's fixed now cheers for the help.

                      • 8. Re: AS2 - Problem with inserting score/name to mysql/database
                        flashyman123

                        hey xmasgame. is it possible to show me your codes or explain how you did it cause im currently having the exact same problem you did and i cant seem to solve it.

                        I can connect to the database but cant send the name and score to it

                        • 9. Re: AS2 - Problem with inserting score/name to mysql/database
                          xmasgame Level 1

                          Hello Flashyman,

                          First of all I want you to take a look at my Game to see how I got the "setup".

                          Click here to view the game

                          Click here to view the highscores

                          Just to make a image of where everything is.

                           

                          This are the most important once (This is all on the same frame):

                          The white box next to "name" have the instance name - "name_txt" (dynamic text)

                          Score = score_txt (dynamic text)

                          submit button = submit_btn

                           

                          The AS that is on the same frame as those 3:

                           

                           

                          stop();

                           

                           

                          if(score_txt.text > lowHighScore){

                              message_txt.text = "Congratulations! Submit your initials and high score..."

                          }else{

                              message_txt.text = "You did not get a high score, please try again!";

                          }

                           

                           

                          xplayer = new LoadVars();

                          xplayerRegistered = new LoadVars();

                          xplayerRegistered.onLoad = showResult;

                           

                          submit_btn.onRelease = submitScore;

                           

                          function submitScore():Void {

                                      xplayer.name = name_txt.text;

                                      xplayer.score = score_txt.text;

                                      xplayer.sendAndLoad("register.php",xplayerRegistered);

                          }

                          function showResult():Void {

                                  message_txt.text = this.error;

                                  message_txt.text = this.message;

                                  name_txt.text = "";

                                  score_txt.text = "";

                                  _root.gotoAndStop(2);

                          }

                           

                          Okey that was the Flash part!

                          Here is the code for the PhP files.

                           

                          get_score2.php

                          <?php

                          $connect = mysql_connect("x", "x", "x");

                          mysql_select_db("x", $connect);

                                  $scores = '';

                                  $count = '';

                           

                                  $query = "SELECT * FROM x order by score DESC LIMIT 0, 10";

                                  $result=mysql_query($query);

                           

                                  $i = 0;

                                  while($row=mysql_fetch_array($result)){

                           

                                      $scores .= 'name'.$i.'='.urlencode($row['name']).'&';

                                      $scores .= 'score'.$i.'='.urlencode($row['score']).'&';

                                      $i++;

                                  }

                                  $count = $i;

                                  echo "count=$count&";

                                  echo $scores;

                          ?>

                           

                          register.php

                          <?php

                           

                          $connect = mysql_connect("x", "x", "x");

                          mysql_select_db("x", $connect);

                           

                          $sql = 'INSERT INTO x (name,score) VALUES ("'.$_POST['name'].'","'.$_POST['score'].'")';

                           

                          $result = mysql_query($sql);

                           

                          if ($result) {

                              $created = 'Score posted for '.$_POST['name'];

                              echo 'message='.urlencode($created);

                          }

                          ?>

                          Hope this solves your problem, hope you got time to reply if it works or not etc

                          • 10. Re: AS2 - Problem with inserting score/name to mysql/database
                            flashyman123 Level 1

                            hey thanks for the speedy reply, i tried to 'imitate' your codes for abit and it seems that it still does not work.

                            The problems are:

                            1) Highscores still do not display

                            2) I am unable to insert any data into the database tables

                             

                            However, problem 2 cannot be due to the php codes as i tried using the .php file by itself and it inserts a row, just that it is without the name and score (for obvious reasons).

                             

                            Hence, its probably that my Flash can't connect to my .php files properly. Any troubleshooting ideas?

                             

                            Thanks!

                             

                            Edit: On reflecting, does the .php file location affect it? both swf/fla and php files are in the same folder as well but do they need to be separated or something?

                            • 11. Re: AS2 - Problem with inserting score/name to mysql/database
                              xmasgame Level 1

                              1. Do you want the highscores to be displayed ingame or at a website like I have ?

                              2. Okey, I migth forgot a few things you actually HAVE to had to make them work.

                                 - I prefer that you copy paste the hole thing because I had lots of trouble trying to change stuff on it myself.

                               

                               

                              Importent to make this script work:

                              Add Dynamic text boxes and name them n0 to n9

                              and another one for s0 to s9 (this is the highscore tables loaded ingame but migth be needed to make the script work actually)

                              one more thing, add a dynamic text box and call it message_txt

                               

                              you can always choose to have this outside of the game cam so people can't see it (or make it invisible), but just have ingame (same frame ofc!) to make the code work

                               

                               

                              If you want flashyman123, I can actually skype with you when I'm home today and help you abit better if you want. But try to add this first and use my use. and BAAAM, maybe it works Be sure your usernames, password etc is correct in the php files etc.

                               

                              Have ur your php files in the same folder

                              • 12. Re: AS2 - Problem with inserting score/name to mysql/database
                                flashyman123.

                                Hi, xmasgame,

                                 

                                I'm flashyman btw. Sorry for taking so long to reply as I was concentrating on my other projects in school.

                                I still haven't got the codes to work and hope that I can chat with you on skype or something. My username is legendpng.

                                If we can't solve the problem, than I'll just do with a highscore board in flash itself which save data in the browser's cookies instead of the database as my deadline is almost up. Is it also possible for me to have the codes of your highscore board as I don't know how to make mine scroll.

                                 

                                Thanks

                                • 13. Re: AS2 - Problem with inserting score/name to mysql/database
                                  xmasgame Level 1

                                  I'm actually not using flash anymore so I haven't used it in a while, if you want Flashyman123 we can talk on skype tonight and we can see what we are able to manage. I wrote a manual and when I read it I guess It will refresh my memory

                                   

                                  Where are you from by the way? I'm thinking about the timezone differens and such.