php - 发布到 SQL 的端点问题

标签 php mysql ajax pdo

我有一个简单的 1 页 web 应用程序,它主要使用 AJAX 来获取/发布,但是,当我尝试在我的一个端点上运行 SQL 时,它会抛出一个内部服务器错误,我想不出为什么,我测试了我在 PHPMyAdmin 中的 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;
});

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

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

最佳答案

尝试更改您的端点代码以遵循本手册: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))) {

关于php - 发布到 SQL 的端点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35736034/

相关文章:

php - 如何使 openssl_encrypt 将输入填充到所需的 block 大小?

php - 查询数据库并返回结果

php - 我在 php 中咨询时遇到困难

mysql - "Unknown column"在 JOIN 中两次使用同一个表时出错

c# - 简化非参数化 mysql 查询

javascript - 从 XMLHttpRequest 中提取单个元素

php - build 中页面的非常简单的订阅表格

php - 请提供数据库架构建议

php - Wordpress 高亮显示当前页面链接

ajax - 如何在本地主机上使用 fetch 发出 CORS 请求?