我有一個簡單的1頁web應用程序,它主要使用AJAX進行GET/POST,但是,當我嘗試在我的一個端點上運行SQL時,它會引發內部服務器錯誤,我想知道爲什麼,我在PHPMyAdmin中測試了我的SQL命令並且它工作正常,我測試了確定我的值正在被捕獲,並且它們是,所以我看不到問題,任何幫助都會很棒,這裏是我的表單:發佈到SQL的端點問題
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="UTF-8">
<title>Add a new album</title>
</head>
<body>
<p class="yeah">Add a new album</p>
<form action="http://localhost:8000/musicOnline/assets/scripts/php/create/new/" method="post" enctype="multipart/form-data">
<input type="text" placeholder="Artist Name" name="artist" required>
<input type="text" placeholder="Album Name" name="album" required>
<input type="text" placeholder="Genre" name="genre" required>
<input type="date" placeholder="Release Date" name="release" required>
<input type="text" placeholder="Record Label" name="recordLabel" required>
<input type="text" placeholder="enter a star rating (1 - 5)" name="rating">
<input type="submit" value="submit">
</form>
</body>
</html>
我的AJAX的處理程序:
//forms that post, put, delete or get
$(document).on("submit", "form", function (e) {
e.preventDefault();
$this = $(this),
$data = $this.serializeArray(),
$method = $this.attr("method"),
$endpointURL = $this.attr("action");
$.ajax({
type: $method,
data: $data,
url: $endpointURL,
success: function (data) {
$("#content-lockup").html(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error: " + textStatus + ", error thrown: " + errorThrown);
}
});
return false;
});
和端點代碼(/資產/腳本/ PHP /創建/新/):
<?php
require "http://localhost:8000/musicOnline/assets/scripts/php/dbConn/index.php";
$artist = $_POST['artist'];
$album = $_POST['album'];
$genre = $_POST['genre'];
$release = $_POST['release'];
$rating = $_POST['rating'];
$recordLabel = $_POST['recordLabel'];
try {
//prepare our statement
$preparedQuery = $conn->prepare("insert into albums (artistName, albumName, genre, releaseDate, recordLabel, rating) values ('" . $artist . "', '" . $album . "', '" . $genre . "', '" . $release . "', '" . $recordLabel . "', '" . $rating . "')");
//execute the statement
if($preparedQuery->execute()) {
echo "success";
$conn = null;
} else {
echo "nope";
$conn = null;
}
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
我的數據庫連接:
<?php
session_start();
//setup variables
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dgc_music_online";
//instantiate our PDO connection to the DB
$conn = new PDO("mysql:host=$servername;dbname=$dbname;", $username, $password);
?>
仍然無效,我添加了更多信息的問題,任何想法? – SkullDev
由於字符串需要「http:// localhost:8000/musicOnline/assets/scripts/php/dbConn/index.php」,可能會出現問題; 因爲你在http上包含文件並且從手冊http://php.net/manual/en/function.include.php中刪除它,這意味着: 這不是嚴格意義上的包含文件並讓它繼承父文件的變量範圍;該腳本實際上正在遠程服務器上運行,結果被包含在本地腳本中。 請重寫要求獲取文件沒有通過http,但只是從您的文件系統 –