我的计划是在顶部放置一个textarea,您可以在其中键入任何内容并将其发布,然后在其下方的两个textarea将更新。但是我不知道该怎么做,请帮忙!

<?php
    if ($_POST['submit']) {
        mysql_connect ("10.246.16.206", "alanay_eu", "hidden")
        or die ('Error: ' . mysql_error());

        mysql_select_db("alanay_eu") or die ('Data error:' . mysql_error());

        $post = mysql_real_escape_string($_POST['post']);

        $query="INSERT INTO Posts (post) VALUES ('$post')";

        mysql_query($query) or die ('Error updating database' . mysql_error());
    }
?>

<center>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <textarea name="post" rows="12" cols="60" spellcheck="false">
      Example Comment</textarea> <br/>
      <input name="submit" type="submit" value="submit" /> <br/>
      <textarea rows="12" cols="60" spellcheck="false"></textarea> <br/>
      <textarea rows="12" cols="60" spellcheck="false"></textarea>
  </form>
</center>

最佳答案

更改表并添加时间戳:

ALTER TABLE Posts ADD postDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP;


然后按postDate排序和限制:

SELECT post FROM Posts ORDER BY postDate DESC LIMIT 2


遍历结果并回显post标记内的textarea

重要

mysql_ *函数已被弃用,不应再使用。查看mysqliPDO

另外,将htmlentities()$_SERVER['PHP_SELF']结合使用可防止XSS。有关更多信息,请参见this question

使用mysqli的示例:

//I usually put the connection in a separate file, and include it as needed
$mysqli = mysqli_connect("myhost","myuser","mypassw","mydb") or die("Error " . mysqli_error($mysqli));

$stmt = $mysqli->prepare("SELECT post FROM Posts ORDER BY postDate LIMIT 2");
$stmt->execute();
$stmt->bind_result($post);
while($stmt->fetch()) {
    $post = htmlentities($post, ENT_QUOTES, "UTF-8");
    echo "<textarea rows='12' cols='60' spellcheck='false'>$post</textarea> <br/>";
}

关于php - 如何使用PHP和MySQL在文本区域中显示两个最新帖子?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23686677/

10-12 12:46
查看更多