Ciao a tutti,
utilizzo questo codice per eseguire una query UPDATE sul mio db.
Codice PHP:
<?php
function RefreshTime($id){
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
} else {echo "conn.ok";}
$time = "";
?>
<script type="text/javascript">
var now = new Date();
var yy = now.getUTCFullYear().toString();
var mm = now.getUTCMonth().toString();
var dd = now.getUTCDate().toString();
var hh = now.getUTCHours().toString();
var min = now.getUTCMinutes().toString();
var sec = now.getUTCSeconds().toString();
var mil = now.getUTCMilliseconds().toString();
if (mm.length.toString()==1){mm="0"+mm;}
if (dd.length.toString()==1){dd="0"+dd;}
if (hh.length.toString()==1){hh="0"+hh;}
if (min.length.toString()==1){min="0"+min;}
if (sec.length.toString()==1){sec="0"+sec;}
if (mil.length.toString()==1){mil="00"+mil;}
if (mil.length.toString()==2){mil="0"+mil;}
var t = yy+mm+dd+hh+min+sec+mil;
<?php $time=t; ?></script>
<?php
$time="<script>document.write(t);</script>";
$query = "UPDATE `devices` SET `currentTime`='" . $time . "' WHERE `_id` ='" . $id . "'";$result = $conn->query($query);
if ($conn->query($query) === TRUE) {
return 1;
} else {
return 0;}
$conn->close();
}
Questa funzione viene eseguita ogni secondo, lo script del tempo restituisce l'orario preciso ai millisecondi, poichè un software su Windows deve poi leggere l'orario dal database in tempo reale per sapere 'l'ultimo accesso'. Il vero problema sta nel fatto che -giustamente- nel database non viene scritto il valore di $time, impostato dallo script, ma semplicemente la stringa "<script>document.write(t);</script>" infatti per quanto io sappia il JS viene eseguito solamente dopo il PHP.
Come fare?? Grazie mille!