2016-03-02 39 views
1

我有一個簡單的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); 

?> 

而且執行console.log的截圖是輸出: error

回答

1

試圖改變自己的端點代碼遵循本手冊:http://php.net/manual/en/pdo.prepared-statements.php

//prepare our statement 
$preparedQuery = $conn->prepare("insert into albums (artistName, albumName, genre, releaseDate, recordLabel, rating) values (?, ?, ?, ?, ?, ?)"); 

//execute the statement 
if($preparedQuery->execute(array($artist, $album, $genre, $release, $recordLabel, $rating))) { 
+0

仍然無效,我添加了更多信息的問題,任何想法? – SkullDev

+0

由於字符串需要「http:// localhost:8000/musicOnline/assets/scripts/php/dbConn/index.php」,可能會出現問題; 因爲你在http上包含文件並且從手冊http://php.net/manual/en/function.include.php中刪除它,這意味着: 這不是嚴格意義上的包含文件並讓它繼承父文件的變量範圍;該腳本實際上正在遠程服務器上運行,結果被包含在本地腳本中。 請重寫要求獲取文件沒有通過http,但只是從您的文件系統 –

0

如果出現這樣的錯誤,應該總是在你的nginx/apache錯誤日誌中的錯誤細節。大多數情況下,有一些有用的記錄。

+0

日誌說,他們無法找到該文件(我認爲它是指db連接文件,你可以看到上面),我做的不明白當我使用絕對鏈接時,看到我的文件夾結構證明它存在:[文件夾結構](https://puu.sh/nrxFB/0449bb3248.png),所以我的2個問題是** 1: **爲什麼它會拋出這個錯誤?和** 2:**是我的數據庫連接的東西看起來好嗎? – SkullDev