我的计划是在顶部有一个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
遍历结果并在textarea
标记内回显post
。
mysql_*函数已被弃用,不应再使用。查看mysqli或PDO
另外,使用htmlentities()
和$_SERVER['PHP_SELF']
来防止XSS。
使用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/>";
}