The form submission code is below. This doesn't include the tutorial feature and allows you to follow along using what I already had created.
- <?php
- // connect
- $connect_error = "Connection error";
- mysql_connect("localhost", "root", "") or die($connect_error);
- mysql_select_db("timesincepost") or die($connect_error);
- $posted = $_POST['posted'];
- $text = addslashes($_POST['text']); // addslashes() function for query security
- $time = time();
- if ($posted=="true")
- {
- $insert_error = "Your text could not be posted";
- $insert = mysql_query("INSERT INTO posts VALUES ('','$time','$text')") or die($insert_error);
- }
- ?>
- <html>
- <body>
- <p />
- <form action="index.php" method="POST">
- Text:<br />
- <textarea name="text"></textarea><br />
- <input type="hidden" name="posted" value="true">
- <input type="submit" value="Post">
- </form>
- </body>
- </html>
- <?php
- ?>
And the code below is the full source code (use it to LEARN!)
- <?php
- // connect
- $connect_error = "Connection error";
- mysql_connect("localhost", "root", "") or die($connect_error);
- mysql_select_db("timesincepost") or die($connect_error);
- $posted = $_POST['posted'];
- $text = addslashes($_POST['text']); // addslashes() function for query security
- $time = time();
- if ($posted=="true")
- {
- $insert_error = "Your text could not be posted";
- $insert = mysql_query("INSERT INTO posts VALUES ('','$time','$text')") or die($insert_error);
- }
- ?>
- <html>
- <body>
- <p />
- <form action="index.php" method="POST">
- Text:<br />
- <textarea name="text"></textarea><br />
- <input type="hidden" name="posted" value="true">
- <input type="submit" value="Post">
- </form>
- </body>
- </html>
- <?php
- $get = mysql_query("SELECT * FROM posts ORDER BY time DESC");
- while ($get_row = mysql_fetch_assoc($get))
- {
- // data
- $get_time = $get_row['time'];
- $get_text = $get_row['text'];
- $diff = $time - $get_time; // seconds
- switch(1)
- {
- case ($diff < 60):
- $count = $diff;
- if ($count==0)
- $count = "a moment";
- else if ($count==1)
- $suffix = "second";
- else
- $suffix = "seconds";
- break;
- case ($diff > 60 && $diff < 3600):
- $count = floor($diff/60);
- if ($count==1)
- $suffix = "minute";
- else
- $suffix = "minutes";
- break;
- case ($diff > 3600 && $diff < 86400):
- $count = floor($diff/3600);
- if ($count==1)
- $suffix = "hour";
- else
- $suffix = "hours";
- break;
- case ($diff > 86400 && $diff < 604800):
- $count = floor($diff/86400);
- if ($count==1)
- $suffix = "day";
- else
- $suffix = "days";
- break;
- case ($diff > 604800 && $diff < 2629743):
- $count = floor($diff/604800);
- if ($count==1)
- $suffix = "week";
- else
- $suffix = "weeks";
- break;
- case ($diff > 2629743 && $diff < 31556926):
- $count = floor($diff/2629743);
- if ($count==1)
- $suffix = "month";
- else
- $suffix = "months";
- break;
- case ($diff > 31556926):
- $count = floor($diff/31556926);
- if ($count==1)
- $suffix = "year";
- else
- $suffix = "years";
- break;
- }
- echo $get_text."<br /> Posted ".$count." ".$suffix." ago <p />";
- }
- ?>
