1. Create a database
The following is the SQL code, it is recommended to create a database with phpMyAdmin.
CREATE DATABASE IF not EXISTSDb_liuyan; UseDb_liuyan;CREATE TABLEly (IDINTEGERUNSIGNED not NULLAuto_increment,usernameVARCHAR( -) not NULL, emailVARCHAR( -) not NULL, ContentTEXT not NULL, DateTIMESTAMP DEFAULT Current_timestamp,PRIMARY KEY(ID)) ENGINE=MyISAMDEFAULT CHARACTER SETUTF8 COLLATE utf8_general_ci auto_increment=0;
The database is simple, there is only one table, one can see that there is no explanation.
2. Configuration (config) section:
For simplicity, all the code in this article is placed in a PHP file. You can detach the code according to the comments in this article.
<? PHP /* * * * Database Setup section, modified according to native device * * * * * * * * * * * * * * * * * * * * */ Define ("Db_host", "localhost"); // Database Server Define ("Db_name", "Db_liuyan"); // Database name Define ("Db_user", "root"); // database user name Define ("Db_password", "root"); // User Password ?>
3. Information Acquisition part
This section is HTML code, user input interface.
<DivID= "form"> <formAction=""Method= "POST"> <P>User name:<inputtype= "text"name= "username" /></P> <P>Mail  Box:<inputtype= "text"name= "Email" /></P> <P>Stay  Statements were<textareaname= "Content" ></textarea></P> <P><inputtype= "Submit"name= "Submit"value= "Submit Message"/></P> </Div> <HR/>
4, message Preservation
Writes the read message content to the database.
<?PHPif(isset($_post[@submit])) { if($_post[@username] &&$_post[@email] &&$_post[@content]) { $content=Str_replace("\ r \ n", "<br/>",$_post[@content]); $content=Str_replace(', ' ',$content); $dsn= ' mysql:host= '. Db_host. '; '. ' Dbname= '.db_name; $pdo=NewPDO ($dsn, Db_user,Db_password); $sql= "INSERT into ly (username,email,content) VALUES ('$_post[Username] ', '$_post[email] ', '$content‘)"; $pdo->query (' Set names UTF8 '); $pdo->query ($sql); } Else{ Echo"The message failed!" <br/> "." Please,try again!; }}
?>
5. Display message
Read the database and display the message content.
<div id= "list" ><?PHP$dsn= ' mysql:host= '. Db_host. '; '. ' Dbname= '.db_name;$pdo=NewPDO ($dsn, Db_user,Db_password);$sql= "SELECT * from-ly ORDER by ID DESC";$pdo->query (' Set names UTF8 ');$query=$pdo->query ($sql); while($row=$query-Fetch ()) { $html= "<p> User name:".$row[@username]. " </p> "." Mail    box: ".$row[@email]. " </p> "." <p> stay    words:<br/> ".$row[@content]. " </p> ".$row[@Date]." ; Echo $html;}?></div>
Now the main function of the message board has been realized, you can test this message board, if the page is garbled, it doesn't matter, continue the following steps can solve the problem.
6, further improve
The following sections of the message version of the page to do a bit of optimization.
First, the HTML code is incomplete, and the standard HTML page should add the following code before the above code.
<!DOCTYPE HTML><HTML><Head><Metaname= "Keywords"content= "PHP Message Board Learning" /><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><Linkrel= "stylesheet"type= "Text/css"href= "Css.css"><title>The Super simple PHP program is perfect for PHP novice practiced hand</title></Head><Body><H3>Would you like to say something?</H3><HR/>
Add the following code at the end of the file.
</ Body > </ HTML >
Then, add CSS styles to make the page more uncluttered and aesthetically pleasing.
We have introduced the Css.css file through the following HTML statement.
<rel= "stylesheet" type= "text/css" href= " Css.css ">
Here's how to write a simple CSS style file (CSS.CSS).
@chartset utf-8;body { margin:100px auto; width:800px; background:url (bg.jpg);} #form textarea { width:300px;} #list { line-height:30px;}
In the CSS style we used a picture (bg.jpg) as the background of the page, the image is stored in the directory, and the name of the bg.jpg is modified.
Now the message board is finished.
This is what we do on the message board:
ly.php