我正在尝试编写基本的论坛系统代码,如果管理员输入消息并将其发布在网站的“新闻”页面上。我设置了“管理面板”,管理员可以在其中键入他们的消息并将其发送到数据库。但是,当我点击Submit时,它不会显示在phpMyAdmin中。我什至尝试对检查“空白字段”和“ sql错误”的if语句进行故障排除。有人知道为什么会这样吗?
输入信息的我的文字表单
<form action="/includes/news_post.php" method="post">
<div class = "container">
<textarea name="message" rows="10" cols="100"></textarea>
<br><br>
<button type="submit" name="news_post" class="postbtn">Post</button>
</div>
</form>
我的用于将消息发送到数据库的php代码。
<?php
if (isset($_POST['news_post'])) {
require 'dbh.inc.php';
$newspost = $_POST['message'];
$uid = $_SESSION['userId'];
if(empty($newspost)) {
header("Location:../admin?error=emptyfield");
exit();
}
else {
$sql = "INSERT INTO news (user_id, post) VALUES (?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header ("Location:../admin?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "ss", $uid, $newspost);
mysqli_stmt_execute($stmt);
header("Location:../admin?news_post=success");
exit();
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else {
header("Location: ../admin?error=unknown");
exit();
}
?>