Php 如何将原始帖子的id分配给帖子回复?
我的数据库中有一个“帖子”表(包含提交的帖子)和一个“回复”表(包含对所有帖子的所有回复) 如果用户回复帖子,我希望数据库中包含回复的行也包含原始帖子的id 除了post_id值(每个条目的值为零)之外,所有内容都输入到数据库中。我怎样才能解决这个问题 home.php:Php 如何将原始帖子的id分配给帖子回复?,php,get,Php,Get,我的数据库中有一个“帖子”表(包含提交的帖子)和一个“回复”表(包含对所有帖子的所有回复) 如果用户回复帖子,我希望数据库中包含回复的行也包含原始帖子的id 除了post_id值(每个条目的值为零)之外,所有内容都输入到数据库中。我怎样才能解决这个问题 home.php: <?php // top_content.php includes the database connection file. include "top_content.php"; // Include our
<?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.";
}
?>
post.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>
我们需要插入一个隐藏字段,并将“id”的变量GET value传递给它
原件:
<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>
您在post.php中的表单中没有“id”字段,并且表单是方法post not GET,因此“reply_parse.php”没有作为“id”获取任何内容。我认为这就是问题所在。我们需要查看调用此代码的代码,以查看ID获得的值。抱歉。我的编辑是否足够?在浏览器中,右键单击网页并选择“源代码”。检查是否确实得到了不同的值。您在post.php中的表单中没有“id”字段,并且表单是方法post not GET,因此“reply_parse.php”没有得到任何作为“id”的内容。我想这就是问题所在。您的表单是method=POST,将其更改为method=GET。或者通过$\u POST[“id”]
更改$\u GET[“id”]
。那么,我有什么事?没有什么?谢谢你,再见?
<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>