php - Action Script 3. How to get time from game and insert to database? -


i'm creating flash game. here timer, counts how long player playing. when "game over" counter stops. , need write time database. counter format mm:ss, don't know how it.

here code send data game php:

var urlreq:urlrequest = new urlrequest ("band.php");  // set method post urlreq.method = urlrequestmethod.post;  // define variables post     var urlvars:urlvariables = new urlvariables(); urlvars.time = timer.currentcount; urlvars.username = "myusername";  // add variables urlrequest urlreq.data = urlvars;    // add urlrequest data new loader var loader:urlloader = new urlloader (urlreq);  // set listener function run when completed loader.addeventlistener(event.complete, onlogincomplete);  // set loader format variables , post php loader.dataformat = urlloaderdataformat.variables; loader.load(urlreq); 

so send time variable php.

here php code time:

<?php  $time = $_post['time']; $username = $_post['username'];      $con=mysqli_connect("localhost","my_db","pass","my_db"); // check connection if (mysqli_connect_errno())   {   echo "failed connect mysql: " . mysqli_connect_error();   }     mysqli_query($con,"insert eurokos (time, username)     values ('$time', '$username')");      $query = "insert eurokos values" . "('$time', '$username')";     echo "success=true";  ?> 

so username sucessfully insterting database, means finy sending variables php, time "0" value, means wrong timer.

my timer looks like:

function showtimepassed(starttime:int):string {    var leadingzeroms:string = ""; //how many leading 0's put in front of miliseconds   var leadingzeros:string = ""; //how many leading 0's put in front of seconds   var leadingzerom:string = "";    var time = gettimer() - starttime; //this gets amount of miliseconds elapsed   var miliseconds = (time % 1000); // modulus (%) gives remainder after dividing,     if (miliseconds < 10) { //if less 2 digits, add leading 0     leadingzeroms = "0";   }    var seconds = math.floor((time / 1000) % 60); //this gets amount of seconds    if (seconds < 10) { //if seconds less 2 digits, add leading 0     leadingzeros = "0";   }    var minutes = math.floor((time / (60 * 1000) ) );     if (minutes < 10) { //if seconds less 2 digits, add leading 0     leadingzerom = "0";   }   //60 seconds times 1000 miliseocnds gets minutes   return leadingzerom + minutes + ":" + leadingzeros + seconds ;      } 

i don't know whats wrong, why time not inserting database. me? thanks.

update

<?php          $time = $_post['time'];     $username = $_post['username'];     session_start(); $name = $_session['vardas'];      $times = gmdate('h:m:s', $time);     $con=mysqli_connect("localhost","padekime_db","pass","my_db"); // check connection if (mysqli_connect_errno())   {   echo "failed connect mysql: " . mysqli_connect_error();   }      $query = "insert eurokos values" . "('$times', '$username')";     echo "success=true"; if ($stmt = $mysqli->prepare("insert eurokos (time, username) value (?,?) ")) {     $stmt->bind_param("i", $time);    $stmt->bind_param("s", $name);    $stmt->execute(); } ?> 

update 2

    function startmemorygame():void     {         addchild(cardcontainer);              timer = new timer(1000); //create new timer ticks every second.             timer.addeventlistener(timerevent.timer, tick, false, 0, true); //listen timer tick             timer.addeventlistener(timerevent.timer, resettimer);             txttime = new textfield();            var format:textformat = new textformat();         format.font = "verdana";         format.color = "#e50041";         format.size = 22;         txttime.border = true;         txttime.bordercolor = 0xffffff;         //format.bold = true;           //txttime.x = 250;         txttime.width/2;            var stagecenter_x:number = stage.stagewidth/2;         var stagecenter_y:number = stage.stageheight/2;          var textcenter_x:number = txttime.width/2;         var textcenter_y:number = txttime.height/2;          txttime.x = stagecenter_x - textcenter_x;         txttime.y = 55;          txttime.autosize = textfieldautosize.center;        txttime.defaulttextformat = format;        message_txt.autosize = textfieldautosize.center;        message_txt.defaulttextformat = format;                          txttime.text = setnull(0);             addchild(txttime);             tmptime = gettimer();             timer.start();             }  private function tick(e:event):void {        txttime.text = showtimepassed(tmptime);                         }     function setnull(starttime:int):string {      return "00:00";     }     function showtimepassed(starttime:int):string {        var leadingzeroms:string = ""; //how many leading 0's put in front of miliseconds       var leadingzeros:string = ""; //how many leading 0's put in front of seconds       var leadingzerom:string = "";        var time = gettimer() - starttime; //this gets amount of miliseconds elapsed       var miliseconds = (time % 1000); // modulus (%) gives remainder after dividing,         if (miliseconds < 10) { //if less 2 digits, add leading 0         leadingzeroms = "0";       }        var seconds = math.floor((time / 1000) % 60); //this gets amount of seconds        if (seconds < 10) { //if seconds less 2 digits, add leading 0         leadingzeros = "0";       }        var minutes = math.floor((time / (60 * 1000) ) );         if (minutes < 10) { //if seconds less 2 digits, add leading 0         leadingzerom = "0";       }       //60 seconds times 1000 miliseocnds gets minutes       return leadingzerom + minutes + ":" + leadingzeros + seconds ;        }      function gettimepassed(starttime:int):number {      return  (gettimer() - starttime) /1000;     } 

here send variable php:

  var urlreq:urlrequest = new urlrequest ("band.php");      // set method post     urlreq.method = urlrequestmethod.post;      // define variables post         var urlvars:urlvariables = new urlvariables();     urlvars.username = 'myusername';      urlvars.time = gettimepassed(starttime); // here error     // add variables urlrequest     urlreq.data = urlvars;        // add urlrequest data new loader     var loader:urlloader = new urlloader (urlreq);      // set listener function run when completed     loader.addeventlistener(event.complete, onlogincomplete);      // set loader format variables , post php     loader.dataformat = urlloaderdataformat.variables;     loader.load(urlreq); } 

the value of time in mysql 0 because mysql receive time string "mm:ss" , data type of field time int.

you have 2 solutions : converts mm:ss number of seconds on php side before insert query or send total of seconds flash side (better pov).

update : elapsed time directly timer instance

[...]

urlvars.time = timer.currentcount; // time elapsed in seconds 

on php side, use prepared statements :

update : full script save time / username in db [...]

$time = $_post['time']; $username = $_post['username'];  $mysqli = new mysqli("localhost","padekime_db","pass","my_db");  if ($stmt = $mysqli->prepare("insert eurokos (time, username) values (?,?) ")) {     $stmt->bind_param('is',$time,$username);     $stmt->execute();     if ($stmt->error != '') {        echo ' error:'.$stmt->error;    } else {        echo 'success';    }    $stmt->close(); } else {    echo 'error:'.$mysqli->error; } 

if trace value of loader.data in onlogincomplete handler, should see "success" or "error:the error description"

http://www.php.net/manual/en/mysqli-stmt.prepare.php


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -