【问题标题】:Endpoint issue for posting to SQL发布到 SQL 的端点问题
【发布时间】:2016-06-14 15:29:41
【问题描述】:

我有一个简单的 1 页 web 应用程序,它主要使用 AJAX 进行 GET/POST,但是,当我尝试在我的一个端点上运行 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截图:

【问题讨论】:

    标签: php mysql ajax pdo


    【解决方案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))) {
    

    【讨论】:

    • 还是不行,我在问题中添加了更多信息,有什么想法吗?
    • 可能因为字符串需要“localhost:8000/musicOnline/assets/scripts/php/dbConn/index.php”而出现问题;因为您通过 http 和手册 php.net/manual/en/function.include.php 包含文件,所以这意味着:严格来说,这与包含文件并使其继承父文件的变量范围不同;该脚本实际上正在远程服务器上运行,然后将结果包含到本地脚本中。请重写 require 以获取文件,而不是通过 http,而只是从您的文件系统获取文件
    【解决方案2】:

    如果出现此类错误,您应该始终在 nginx/apache 错误日志中提供错误详细信息。大多数时候都会记录一些有用的东西。

    【讨论】:

    • 日志说他们找不到文件(我认为它指的是db连接文件,您可以在上面看到),当我使用绝对链接时我不明白,请参阅我的文件夹结构在这里作为它存在的证据:folder structure,所以我的 2 个问题是 1: 为什么会抛出这个错误? 2: 从你所看到的来看,我的数据库连接是否正常?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多