[英]How to make users posts to appear on a page?
I want every time someone posts something that post to appear below the one that already exists.我希望每次有人发布的内容都显示在已经存在的内容下方。 If I use this it only replace the last post.
如果我使用它,它只会替换最后一个帖子。 How should I do it?
我该怎么做?
<?php
session_start();
$db = mysqli_connect("localhost", "root", "", "store");
if (isset($_POST['button'])) {
session_start();
$title= mysqli_real_escape_string($db, $_POST['title']);
$body= mysqli_real_escape_string($db, $_POST['body']);
$sql = "INSERT INTO content (title,body) VALUES ('$title', '$body')";
mysqli_query($db, $sql);
$_SESSION["title"] = $title;
header("location: home.php");
}
?>
And on the page that I want to display:在我想显示的页面上:
<?php
session_start();
php echo $_SESSION["title"];
?>
You have to redirect the page to the home.php
on any condition but you have not done that.您必须在任何情况下将页面重定向到
home.php
,但您还没有这样做。
if mysqli_query()
after execution returns FALSE
or TRUE
the header location will be done and that is not advisable.如果
mysqli_query()
执行后返回FALSE
或TRUE
则标头位置将完成,这是不可取的。 Since you are needing to echo
the SESSION variable
over to the home.php
page.因为您需要将
SESSION variable
echo
显到home.php
页面。
You can check condition like this if needed.如果需要,您可以像这样检查条件。
if(mysqli_query($db, $sql))
{
// Here you can handle the TRUE loop
$_SESSION["title"] = $title;
header("location: home.php");
}
else
{
// Here you can handle the FALSE loop
}
And in the home.php
you have make the following modifications in order to display the session variable to print.在
home.php
您进行了以下修改以显示要打印的会话变量。
Check Points to print the session variable in other page
检查点在其他页面打印会话变量
session_start()
at the top of the page since you need to get the super global variable over to that page.session_start()
,因为您需要将超级全局变量转移到该页面。ob_start()
ob_start()
的帮助下重新启动输出缓冲ob_start()
must be used only if you get header already sent error
in the redirection problems. ob_start()
必须仅在重定向问题中收到header already sent error
时使用。 Usage of ob_start()
: ob_start()
用法:
Thinking of ob_start()
as saying "Start remembering everything that would normally be outputted, but don't quite do anything with it yet."将
ob_start()
视为说“开始记住通常会输出的所有内容,但还没有对它做任何事情。”
Example:例子:
ob_start();
echo("Welcome User!"); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();
ob_start()
- Turn on output buffering ob_start()
- 打开输出缓冲
This function will turn output buffering on.此功能将打开输出缓冲。 While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
当输出缓冲处于活动状态时,脚本不会发送任何输出(标题除外),而是将输出存储在内部缓冲区中。
The contents of this internal buffer may be copied into a string variable using ob_get_contents()
.可以使用
ob_get_contents()
将此内部缓冲区的内容复制到字符串变量中。 To output what is stored in the internal buffer, use ob_end_flush().要输出存储在内部缓冲区中的内容,请使用 ob_end_flush()。 Alternatively,
ob_end_clean()
will silently discard the buffer contents.或者,
ob_end_clean()
将默默地丢弃缓冲区内容。
Returns TRUE on success or FALSE on failure.成功时返回TRUE ,失败时返回FALSE 。
And over to the question you have made you have to make some changes
关于你提出的问题,你必须做出一些改变
You have started the session
and that is good but you have appended the php
to the echo statement and that looks incorrect.您已经启动了
session
,这很好,但是您已将php
附加到 echo 语句,这看起来不正确。
Replace:代替:
php echo $_SESSION["title"];
with:和:
echo $_SESSION["title"];
Rather all the other codes are fine.相反,所有其他代码都很好。
And to the whole the Entire page will look like this.整个页面看起来像这样。
<?php
session_start();
$db = mysqli_connect("localhost", "root", "", "store");
if (isset($_POST['button'])) {
//session_start(); This is not needed at all already you have started the session.
$title= mysqli_real_escape_string($db, $_POST['title']);
$body= mysqli_real_escape_string($db, $_POST['body']);
$sql = "INSERT INTO content (title,body) VALUES ('$title', '$body')";
mysqli_query($db, $sql);
$_SESSION["title"] = $title;
header("location: home.php");
}
?>
home.php主页.php
<?php
session_start();
echo $_SESSION["title"];
?>
Note: Your Code is at SQL Injection vulnerability it is advised to use of the prepared statements along with
mysqli.*
注意:您的代码存在 SQL 注入漏洞,建议将准备好的语句与
mysqli.*
一起使用mysqli.*
Prepared Statements Reference for Mysqli: - http://php.net/manual/en/mysqli.prepare.php . Mysqli 的准备语句参考: - http://php.net/manual/en/mysqli.prepare.php 。
I think my explanation would be clear for you to rectify your Error and code pretty good.我认为我的解释对您纠正错误和代码很清楚。
Thanks & Happy coding:)谢谢&快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.