Here are the solutions to the previous tutorial, ‘Briefing: Using MySQL’. If you missed this tutorial, you can find it here.

(note that there is more than one way to complete a task – I’m just giving you an idea of how to do it)
Task One
The first task asked you to set up a table. In the end, here’s the query that you should have run:
CREATE TABLE `database`.`table` ( `body` TEXT NOT NULL , `time` INT( 12 ) NOT NULL , `ip_address` VARCHAR( 16 ) NOT NULL )
You could have ran it with phpMyAdmin. That is pretty straight forward, but here‘s an example of what it could have looked like.
Task Two
The second task asked you to create a simple input that, when submitted, is put into the database. First, we’re going to need a script to connect to the database.
connect.php
<?php mysql_connect( 'localhost', 'username', 'password' ) or die( mysql_error() ); mysql_select_db( 'database' ) or die( mysql_error() ); ?>
You can include (or require) this script in the other scripts as needed.
form.php
<html> <body> <form method="post" action="submit.php"> Comment: <input type="text" name="comment" /><br /> <input type="submit" value="Submit" /> </form> </body> </html>
submit.php
<?php
require_once( 'connect.php' ); // You need to include this sometime, it doesn't have to be here. If you are using a separate configuration file, you may want to have it work with that.
if( isset( $_POST['comment'] ) ) {
$comment = mysql_real_escape_string( $_POST['comment'] );
$time = time();
$query = "INSERT INTO table (body, time, ip_address) VALUES ('{$comment}', '{$time}', '{$_SERVER['REMOTE_ADDR']}')";
mysql_query( $query );
}
echo "<a href=\"form.php\">form.php</a>";
?>
You can view this solution in action here.
Task Three
The third task only requires a small modification to one file.
form.php
<html>
<body>
<form method="post" action="submit.php">
Comment: <input type="text" name="comment" /><br />
<input type="submit" value="Submit" /><br />
</form>
<?php
$query = "SELECT * FROM table ORDER BY time DESC LIMIT 10";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) ) {
echo stripslashes( $row['body'] );
echo ' posted on ';
echo date( 'n/j/y \a\t g:i' );
echo '<br />';
}
?>
</body>
</html>
You can view this solution in action here.