我正在尝试使用POST
and将数据保存到数据库中AJAX
。该脚本作为 Firefox 的用户脚本运行。当我运行脚本时,没有显示任何错误,数据库中也没有任何内容。
PHP
$db_connection = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
//$db_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
//$db_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
//$db_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$insert_key = $db_connection->prepare("INSERT into userstats (key, match, expire) VALUES (?, ?, ?, ?)");
$insert_key->bindParam(1, $_POST["user"]);
$insert_key->bindParam(2, $_POST["score"]);
$insert_key->bindParam(3, $_POST["location"]);
$insert_key->bindParam(4, $_POST["pointtime"]);
$insert_key->execute();
$db_connection = null;
echo "saved to database!";
} catch(PDOException $e) {
echo $e->getMessage();
}
javascript
xhr = new XMLHttpRequest();
xhr.open("POST", "stats.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
}
var params = "?user=james&score=5&location=homepage&pointtime=1350249055";
xhr.send(params);