[英]Set username as author of comment in php/mysql
因此,我正在創建一個網站,注冊用戶可以在該網站上發表帖子、評論帖子以及回復評論,但我無法顯示評論過的用戶名。
文件 home.php
<?php
$sql = "SELECT * FROM post LIMIT 3";
$result = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result)){
$id = $row['users_id'];
$sql2 = "SELECT * FROM users WHERE users_username='$id'";
$result2 = $conn->query($sql2);
if($row2 = mysqli_fetch_assoc($result2)){
echo "<div class='comment-box'>";
echo $row['title']."<br>";
echo $row['date_created']."<br>";
echo $row2['users_id']."<br>";
echo $row['content']."<br>";
echo "</p></div>";
if(isset ($_SESSION["username"])){
echo "<button type='button' id='postbtn' onclick='replyFunction()'>Reply</button>";
include 'commentsection.php';
}
}
}
文件 makeapost.php
<div class="thread">
<form action="<?php echo htmlspecialchars("includes/posts.inc.php");?>"method="post">
<h4>Create a post </h4>
<hr></hr>
<input type="hidden" name="post_id">
<input type="hidden" name="users_id" value="<?php echo ".$_SESSION[username].";?>">
<input type="text" id="thetitle" name="title" placeholder="Title">
<input type="hidden" name="date_created"> <br>
<textarea id="summernote" name="content"></textarea>
<hr></hr>
<button type="submit" id="postbutton" name="submit">Post</button>
</form>
</div>
文件 post.inc.php
<?php
include 'dbh.inc.php';
include 'functions.inc.php';
if (isset($_POST["submit"])){
$title = $_POST["title"];
$users_id = $_POST["users_id"];
$content = $_POST["content"];
$date_created = $_POST["date_created"];
$mysqltime = date ('Y-m-d H:i:s');
$sql = "INSERT INTO post (title, users_id, content, date_created) VALUES (?,?,?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)){
header("location: ../home.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ssss", $title, $users_id, $content, $mysqltime);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../home.php?error=none");
exit();
}
登錄.inc.php
<?php
if (isset($_POST ["submit"])){
$username = $_POST["username"];
$password = $_POST["password"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if(emptyInputLogin($username, $password) !== false){
header("location: ../home.php?error=emptyinput");
exit();
}
loginUser($conn, $username, $password);
} else {
header("location: ../home.php");
exit();
}
函數.inc.php
function loginUser($conn, $username, $password){
$uidExists = uidExists($conn, $username, $username);
if($uidExists === false){
header("location: ../login.php?error=wrongusername");
exit();
}
$passwordHashed = $uidExists["users_password"];
$checkPwd = password_verify($password, $passwordHashed);
if($checkPwd === false){
header("location: ../login.php?error=wrongpassword");
exit();
} else if ($checkPwd === true){
session_start();
$_SESSION["userid"] = $uidExists["users_id"];
$_SESSION["username"] = $uidExists["users_username"];
$_SESSION["role"] = $uidExists["users_role"];
header("location: ../home.php");
exit();
}
}
表結構
CREATE TABLE IF NOT EXISTS `post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`users_id` int(11) NOT NULL,
`content` varchar(500) NOT NULL,
`date_created` varchar(500) NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14;
CREATE TABLE IF NOT EXISTS `users` (
`users_id` int(11) NOT NULL AUTO_INCREMENT,
`users_username` tinytext NOT NULL,
`users_password` longtext NOT NULL,
PRIMARY KEY (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
這可能與我無法將正確的用戶 ID 從 post 表連接到 users 表這一事實有關,因為在 users 表中我有諸如 (users_id(AUTO_INCREMENT), users_username) 之類的列,但在 post 表中我有(用戶 ID)。 我可以發帖並將消息插入數據庫,但沒有顯示用戶。
關於如何改進這一點的任何提示? 現在 home.php 頁面上沒有顯示任何內容,但帖子仍在數據庫中。
根據您在上面發布的表定義(與您的 PHP 代碼不匹配),我創建了以下數據:
帖子表:
post_id | users_id | 內容 | 創建日期 |
---|---|---|---|
14 | 6 | 這里有一些內容 | 2022 年 5 月 20 日 |
15 | 7 | 一個消息 | 2022 年 5 月 19 日 |
16 | 7 | 另一條消息 | 2022 年 5 月 20 日 |
17 | 8 | 我無話可說 | 2022 年 5 月 20 日 |
18 | 9 | 我已經把一切都說了。 | 2022 年 5 月 18 日 |
用戶表
users_id | users_username | 用戶密碼 |
---|---|---|
6 | 麥克風 | 密碼 |
7 | 喬治 | 另一個密碼 |
8 | 海倫 | 密碼 |
9 | 愛麗絲 | 密碼 |
10 | 鮑勃 | 秘密 |
從這里我創建了這個查詢:
select post_id, content, date_created, u.users_username
from
post
join
(select users_username, users_id from users) u
on post.users_id = u.users_id
order by date_created desc
limit 3;
這給出了這個結果:
post_id | 內容 | 創建日期 | users_username |
---|---|---|---|
14 | 這里有一些內容 | 2022 年 5 月 20 日 | 麥克風 |
16 | 另一條消息 | 2022 年 5 月 20 日 | 喬治 |
17 | 我無話可說 | 2022 年 5 月 20 日 | 海倫 |
現在,您可以循環遍歷結果並創建輸出,而無需嵌套查詢來檢索數據。
筆記:
dateCreated
應該是DateTime
列,而不是varchar
列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.