简体   繁体   English

如何让用户帖子出现在页面上?

[英]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()执行后返回FALSETRUE则标头位置将完成,这是不可取的。 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检查点在其他页面打印会话变量

  1. Ensure that you have session_start() at the top of the page since you need to get the super global variable over to that page.确保您在页面顶部有session_start() ,因为您需要将超级全局变量转移到该页面。
  2. If you need to show anything that is queried from the DB you need to ensure the connection of DB over to that page.如果您需要显示从数据库查询的任何内容,您需要确保数据库连接到该页面。
  3. If any header Location error occurs try to restart your output buffering over to the page where you have started you header location.如果发生任何标题位置错误,请尝试重新启动输出缓冲到您开始标题位置的页面。 You can restart your output buffering with the help of ob_start()您可以在ob_start()的帮助下重新启动输出缓冲
  4. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM