|
|
|
|
How to create simple forum in PHP from scratch
In this lesson we will cover most of the basic PHP knowledge and will learn how to create a very simple forum by using PHP and MySql database.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.
|
|
|
First we will create MySql database for storing data with the following fields:
id - int user - text message - text date - datetime
You can read more about creating database in tutorial:
|
|
|
2.
|
|
|
Now we will create a form for posting messages.
The following code should be put between <head> and </head> tags.
<form method="get" action="forum.php"> <p>User: <label for="user"></label> <input type="text" name="user" id="user" /> <br /> </p> <p>Message: <br /> <label for="message"></label> <textarea name="message" id="message" cols="45" rows="5"></textarea> </p> <p> <input type="submit" name="submit" id="submit" value="Post message" /> </p> </form>
To learn about forms and PHP, please see tutorial:
|
|
|
3.
|
|
|
You should get a result that is shown on the picture.
|
|
|
4.
|
|
|
At the start of the script we will first connect to the database.
Insert the code below at the top of your php script>
<? $mysqli = new mysqli("localhost", "root", "", "my_db"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } ?>
You should also set correct hostname, username, password and database name for your database.
You can learn more about using PHP and MySql in tutorial:
|
|
|
5.
|
|
|
After establishing connection to MySql database we will check if there was new message posted.
If this is the case we will insert the data into our database.
if (isset($_GET['message'])) { $user=$mysqli->real_escape_string($_GET['user']); $message=$mysqli->real_escape_string($_GET['message']); $date=date('Y-m-d H:i:s'); $sql="INSERT INTO forum(id, user, message, date) VALUES(0,'$user','$message','$date')"; $mysqli->query($sql); }
|
|
|
6.
|
|
|
Now all that is left to do is to show all the messages from the database.
This code should be placed after the <body> tag.
<? $sql = "SELECT * FROM forum"; $result = $mysqli->query($sql);
while($row = $result->fetch_assoc()) { echo $row['user'].', '.$row['date'].' <br />'; echo $row['message'].'<br />'; echo '------------------------ <br />'; } ?>
To learn more about SQL, please see tutorial:
|
|
|
7.
|
|
|
Now your simple forum is ready and you can use it to post messages.
Congratulations.
Below is also a complete code so it will be easier for you to copy it to your php script:
<? $mysqli = new mysqli("localhost", "root", "", "my_db"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } if (isset($_GET['message'])) { $user=$mysqli->real_escape_string($_GET['user']); $message=$mysqli->real_escape_string($_GET['message']); $date=date('Y-m-d H:i:s'); $sql="INSERT INTO forum(id, user, message, date) VALUES(0,'$user','$message','$date')"; $mysqli->query($sql); }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP and MySql</title> </head>
<body> <h2>My First Forum</h2>
<? $sql = "SELECT * FROM forum"; $result = $mysqli->query($sql);
while($row = $result->fetch_assoc()) { echo $row['user'].', '.$row['date'].' <br />'; echo $row['message'].'<br />'; echo '------------------------ <br />'; } ?>
<form method="get" action="forum.php"> <p>User: <label for="user"></label> <input type="text" name="user" id="user" /> <br /> </p> <p>Message: <br /> <label for="message"></label> <textarea name="message" id="message" cols="45" rows="5"></textarea> </p> <p> <input type="submit" name="submit" id="submit" value="Post message" /> </p> </form>
</body> </html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this is excellent..... simple and straight forward
|
|
|
|
|
|
|
|
|
Ohhh this is excellent...You just saved me... I am taking a PHP course and I have an assignment which requires exactly what this tutorial is about...I had no idea how to solve it. Thank you very much!
|
|
|
|
|