gpt4 book ai didi

php - 发布到 SQL 的端点问题

转载 作者:行者123 更新时间:2023-11-28 23:35:58 25 4
gpt4 key购买 nike

我有一个简单的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com