我以以下方式创建了database.db并创建了一个表:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('database.db');
}
}
$db = new MyDB();
if (!$db) {
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql = <<<EOF
CREATE TABLE EVENTS
(ID INTEGER PRIMARY KEY AUTO_INCREMENT,
TITLE char(50) NOT NULL,
DATE CHAR(50) NOT NULL,
LOCATION CHAR(50),
DESCRIPTION CHAR(50));
EOF;
$ret = $db->exec($sql);
if (!$ret) {
echo $db->lastErrorMsg();
} else {
echo "Table created successfully\n";
}
$db->close()
我的请求:
exploreButton.addEventListener("click", () => {
sectionArticles.style.display = "flex";
window.scroll({
top: window.innerHeight,
left: 0,
behavior: "smooth"
});
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlttp.responseText);
}
};
xmlhttp.open("GET", "server/get.php", true);
xmlhttp.send();
});
我的POST请求:
publishButton.addEventListener("click", evt => {
evt.preventDefault();
let formData = new FormData();
formData.append("title", document.querySelector("#title").value);
formData.append("date", document.querySelector("#date").value);
formData.append("location", document.querySelector("#location").value);
formData.append("type", document.querySelector("#type").value);
formData.append("description", document.querySelector("#description").value);
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
};
xmlHttp.open("POST", "server/publish-articlesql.php");
xmlHttp.send(formData);
});
这是server / publish-articlesql.php页面的外观:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('database.db');
}
}
$db = new MyDB();
if (!$db) {
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$title = $_POST["title"];
$date = $_POST["date"];
$location = $_POST["location"];
$type = $_POST["type"];
$description = $_POST["description"];
$sql = <<<EOF
INSERT INTO EVENTS (TITLE,DATE,LOCATION,DESCRIPTION)
VALUES ({$title}, {$date}, {$location}, {$description} );
EOF;
$ret = $db->exec($sql);
if (!$ret) {
echo $db->lastErrorMsg();
} else {
echo "Records created successfully\n";
}
$db->close();
对于有经验的人,我在做什么错?在我的情况下,处理POST / GET请求的正确方法是什么?每当我发出这些请求时,都会收到500:Internal Server Error。 javascript代码在MySQL上可以很好地工作,但是由于我不得不改用SQLite,所以我不知道如何使JS代码适应它。有人可以向我解释如何吗? 非常感谢。