gpt4 book ai didi

php - 如何修复 stmt->execute() 总是返回 false?

转载 作者:行者123 更新时间:2023-11-30 21:57:46 25 4
gpt4 key购买 nike

我正在开发一个带有数据库的 php 项目。我希望用户能够评论文章(我已经有一个用于文章创建的工作系统)但我无法将评论插入数据库。这是代码:

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
$content = filter_input(INPUT_POST, 'new_entry', FILTER_SANITIZE_STRING);
$author = $_SESSION["username"];
$article_id = $_GET['article'];
$rating = 0;

if ($insert_stmt = $mysqli->prepare("INSERT INTO `entries` (`article_id`, `rating`, `content`, `author`) VALUES (?, ?, ?, ?)")) {
$insert_stmt->bind_param('iiss', $article_id, $rating, $content, $author);
// Führe die vorbereitete Anfrage aus.
if ($insert_stmt->execute()) {
header('Location: success.php');
} else {
header('Location: failure.php');
}
}

这总是引导我到“failure.php”——这里有什么问题?

先谢谢你了!

最佳答案

因为你有,

$article_id = $_GET['article'];

在您的代码中,当您发布表单数据时,
所以您的 $articlenull""。这就是它重定向到 failure.php 的原因。

改为使用,

$article_id = $_POST['article'];

关于php - 如何修复 stmt->execute() 总是返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481973/

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