发布到SQL的端点问题

时间:2016-03-02 00:01:40

标签: php mysql ajax pdo

我有一个简单的1页webapp,它主要使用AJAX进行GET / POST,但是,当我尝试在我的一个端点上运行SQL时,它会引发内部服务器错误,我想不出为什么,我测试过我在PHPMyAdmin中的SQL命令并且它有效,我测试以确保我的值被捕获,它们是,所以我看不到问题,任何帮助都会很棒,这是我的表格:

<div class="alert alert-info" 
     ng-show="filtered_result.length == 0">
    No items in the collection!
</div>

我的AJAX处理程序:

<!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>

端点代码(/ assets / scripts / php / create / new /):

//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

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();
}

?>

输出的console.log的屏幕截图: error

2 个答案:

答案 0 :(得分: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))) {

答案 1 :(得分:0)

如果出现类似错误,您应该始终在nginx / apache错误日志中记录错误详细信息。大部分时间都有一些有用的记录。