5 minutes to write a PHP message board program

Source: Internet
Author: User
Tags dsn

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&nbsp&nbspBox:<inputtype= "text"name= "Email" /></P>            <P>Stay&nbsp&nbspStatements 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(', ' &nbsp; ',$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" &GT;&LT;?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 &nbsp&nbsp box: ".$row[@email]. " </p> "." <p> stay &nbsp&nbsp 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.