Question

My plan was to have one textarea at the top where you could type whatever you want and post it, then the two textarea's below it would update. But I have no idea how to do this, please help!

<?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>
Was it helpful?

Solution

Alter the table and add a timestamp:

ALTER TABLE Posts ADD postDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

Then order and limit by postDate:

SELECT post FROM Posts ORDER BY postDate DESC LIMIT 2

Iterate through the results and echo out the post inside the textarea tag.

IMPORTANT

mysql_* functions are deprecated, and should no longer be used. Look into mysqli or PDO.

Also, use htmlentities() with $_SERVER['PHP_SELF'] to protect against XSS. See this question for more info.

An example using 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/>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top