javascript - Update MYSQL value every second on the server -
javascript - Update MYSQL value every second on the server -
i trying create script refreshes value in database bound specific registered user. different each user ofc.
so example: have account, , business relationship has row in database called credit, , credit should increment every sec registration. (even if business relationship logged out.)
let's $credit = $credit + 1; (every second).
this current code:
<html> <head> <script type="text/javascript"> function updatedb() { if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("result").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","main.php",true); xmlhttp.send(); settimeout(updatedb, 1000); } </script> </head> <body onload=updatedb()> <span id="result"><?php $username = $_session['username']; $query = "select username, credit users username = '$username'"; $updater = "update users set credit=credit+1 username = '$username'"; $data = mysqli_query($dbc, $query); mysqli_query($dbc, $updater); if (mysqli_num_rows($data) == 1) { $row = mysqli_fetch_array($data); $credit= $row['credit']; } echo "<br><br>user: " . $username . ""; echo "<br>"; echo "credit: ". $credit.""; ?> </span> </body> </html> the problem code that, refreshes value while business relationship logged in.
if want count time user on site use:
<script> var starttime = new date().gettime(); // time in milliseconds window.onunload = function() { var finishtime = new date().gettime(); // time in milliseconds ajaxcall(finishtime-starttime); // abstract ajax phone call } </script> use on every page. ajax phone call should update database amount of time user on page.
http://reference.sitepoint.com/html/event-attributes/onunload
javascript php mysql
Comments
Post a Comment