我有一个'帖子'表格(包含提交的帖子)和“回复”#39; table(包含对所有帖子的所有回复)在我的数据库中。
如果用户回复帖子,我希望包含回复的数据库中的行也包含原始帖子的ID。
除了post_id值(每个条目为零)之外,所有内容都输入到数据库中。我该如何解决这个问题?
home.php:
head = dummy->next;
post.php中:
<?php
// top_content.php includes the database connection file.
include "top_content.php";
// Include our script to convert MySQL timestamp to 'ago' format.
include "time_ago.php";
// Create an object for the time conversion functions
$timeAgoObject = new convertToAgo;
include "menu_and_logo.php";
// Query the database for all submitted posts. //
$sql = "SELECT * FROM Posts ORDER BY date DESC";
// Store the result in a variable. //
$res = mysqli_query($db, $sql) or die(mysqli_error);
// Check that the result has > 0 rows; that at least one post exists in the database. //
if(mysqli_num_rows($res) != 0) {
// The mysqli_fetch_array retrieves and returns the next row of our query, which is then assigned to $row. //
// Then it executes the echos, and the procedure begins anew. This is how we get the post list. //
while($row = mysqli_fetch_array($res)) {
// Set the post_id variable equal to the post_id of each post. //
$post_id = $row['post_id'];
$ts = $row['date'];
$post_title = $row['post_title'];
$post_creator = $row['post_creator'];
// Convert Date Time
$convertedTime = ($timeAgoObject -> convert_datetime($ts));
// Then convert to 'ago' time
$when = ($timeAgoObject -> makeAgo($convertedTime));
// Display the data of each row. THe while row will execute all posts, creating a list
// display.
echo '<div>';
// The text is set as the post title, and points to post.php?id=*the post_id of the post. //
// In post.php, we check that $_GET['id'] is set (that the user is visiting post.php?id=some_integer),
// then query the database for the respective post, and echo the relevant content. //
echo '<a href="post.php?id='.$post_id.'">'.$post_title.'</a>';
echo '<p>by <a href = "view_profile.php?user='.$post_creator.'">'.$post_creator.'</a> '.$when.'</p>';
echo '</div>';
}
}else {
echo "There are no posts.";
}
?>
reply_parse.php:
<!DOCTYPE html>
<?php
session_start();
// Include our script to convert MySQL timestamp to 'ago' format.
include "time_ago.php";
// Create an object for the time conversion functions
$timeAgoObject = new convertToAgo;
// Iniate connection to the database.
include "db_connect.php";
// Check that the user is visiting post.php?id=some_integer).
if($_GET['id']) {
// Set the post id as a variable, for convenience.
$post_id = $_GET['id'];
// Query the database for the post that corresponds to the post-title link that has been clicked.
$sql = "SELECT * FROM Posts WHERE post_id = '".$post_id."' ";
$res = mysqli_query($db, $sql)or die(mysqli_error());
// Check that there exists a row containing the relevant post id.
if(mysqli_num_rows($res) != 0) {
// Store the current row's array of data as a variable.
while($row = mysqli_fetch_array($res)) {
// Set the current row's data as variables.
$post_title = $row['post_title'];
$post_content = $row['post_content'];
$post_creator = $row['post_creator'];
$ts = $row['date'];
// Convert Date Time
$convertedTime = ($timeAgoObject -> convert_datetime($ts));
// Then convert to ago time
$when = ($timeAgoObject -> makeAgo($convertedTime));
// Display the relevant post data.
echo '<h2>'.$post_title.'</h2>';
echo '<p>Submitted by <a href = "view_profile.php?user='.$post_creator.'">'.$post_creator.'</a> '.$when.'</p><nr><br>';
echo ''.$post_content.'';
}
}else{
echo "This post does not exist.";
}
}else{
header("home.php");
}
?>
<!-- I've moved the html tags here because the file needs the $post_title variable before setting the title -->
<html>
<head><title><?php echo ''.$post_title.' - Lboro Maths'; ?></title></head>
<body>
<!-- #2: The form where users can submit replies to the original post. -->
<form action="reply_parse.php" method="POST">
<input type="textarea" name="reply_content" placeholder="Post a reply...">
<input type="submit" name="submit_reply" value="Reply">
</form>
</body>
</html>
答案 0 :(得分:1)
我们需要插入一个隐藏字段并将“&#39; id&#39;”的变量GET值传递给它。
原件:
<body>
<!-- #2: The form where users can submit replies to the original post. -->
<form action="reply_parse.php" method="POST">
<input type="textarea" name="reply_content" placeholder="Post a reply...">
<input type="submit" name="submit_reply" value="Reply">
</form>
</body>
修正:
<body>
<!-- #2: The form where users can submit replies to the original post. -->
<?php
echo "<form action='reply_parse.php' method='POST'>";
echo "<input type='hidden' name ='post_id' value='$post_id'>";
echo "<input type='textarea' name='reply_content' placeholder='Post a reply...'>";
echo "<input type='submit' name='submit_reply' value='Reply'>";
echo "</form>";
?>
</body>
答案 1 :(得分:0)
你没有&#34; id&#34;在post.php中你的表单中的字段,表单是POST而不是GET的方法,所以&#34; reply_parse.php&#34;正在变得没有&#34; id&#34;。我认为这就是问题所在。