我有一些培训视频,其中我每秒更新一次数据库。我从 JS 到 PHP 到 Mysql 的工作代码经过了几秒钟的观察。我遇到的问题是,除非我对每个请求(每秒)开始一个新连接,否则它不会更新。有没有更好的办法?我的 JS 代码;
//AJAX save 'seconds played' values to database
function updatedb() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.open("GET","http://localhost/userupdate.php?q=" + userID + "&r=" + (medeno[index][0]) + "&s=" + index, true);
xmlhttp.send();
}
和我的 PHP 代码(单独的 file.php);
<?php
//Get values
$userID=$_GET['q'];
$secc=$_GET['r'];
$index=$_GET['s'];
$userID=(int)$userID;
$secc=(int)$secc;
$index=(int)$index;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "medeno_user", "cr3134", "medeno_users");
//Update database 1 case per video course
switch ($index) {
case 1: $stmt1=$mysqli->prepare('UPDATE users set videoSec1=? WHERE userID=?'); break;
case 2: $stmt1=$mysqli->prepare('UPDATE users set videoSec2=? WHERE userID=?'); break;
case 3: $stmt1=$mysqli->prepare('UPDATE users set videoSec3=? WHERE userID=?'); break;
case 4: $stmt1=$mysqli->prepare('UPDATE users set videoSec4=? WHERE userID=?'); break;
case 5: $stmt1=$mysqli->prepare('UPDATE users set videoSec5=? WHERE userID=?'); break;
case 6: $stmt1=$mysqli->prepare('UPDATE users set videoSec6=? WHERE userID=?'); break;
case 7: $stmt1=$mysqli->prepare('UPDATE users set videoSec7=? WHERE userID=?'); break;
case 8: $stmt1=$mysqli->prepare('UPDATE users set videoSec8=? WHERE userID=?'); break;}
$stmt1->bind_param('ii',$secc,$userID);
$stmt1->execute();
$stmt1->close();