Recently read PHP basic grammar, want to use these basic things to implement the message board, but also a solid foundation of knowledge.
What is a message board? A carrier that can be used to record and display text messages.
Now cut to the point, say this message board is how to achieve!
First, the user submits the message, the relevant content into the server, when he wants to see the backstage and then all the messages read out, finally displayed in the browser, the user can see the message.
This one of the backstage needs a tool that is easy to read and write data, I choose MySQL database to help me accomplish these things.
I wrote the main is three PHP files, respectively:
conn.php connection database;
addmsg.php PHP read the message from the page, and put it into the database (Insert);
Listmsg.php reads the message from the database and displays it on the page;
1. To prepare the structure of the database table, here is my table structure under phpMyAdmin:
Build table Syntax
SQL CREATE TABLE Syntax
CREATE table table name (column name 1 data type, column name 2 data type, column name 3 data type,....)
2.php connect to MySQL database, and then select one of the databases, I chose here is the BBS database (PS created before) The following describes a few PHP library functions to use,
①mysql_connect ("localhost", "root", "" ")
PHP connection MySQL, the parameters are MySQL address (localhost for natively), user name, password
Return value: If the connection fails to return FALSE, a connection identifier is returned successfully
②mysql_select_db ($dbName, $conn);
MySQL can have a lot of db, so you need to select one of the DB for the next operation.
Parameters: The first is the database name, the second is the link identifier, you can put the return value in ① here, which means I will use MySQL in ①.
Return value: False connection failed, true connection succeeded.
③mysql_query (query,connection)
Parameter: Query represents the statement you want MySQL to execute
Connection optional, SQL connection identifier as above
return value: mysql_query () returns a resource identifier only for a select,show,explain or describe statement, or FALSE if the query is executed incorrectly.
For other types of SQL statements, mysql_query () returns TRUE on successful execution and returns FALSE on error.
Personal summary of this return value: This function fails to return false, execution succeeds to see what statement, if it is a select,show,explain or DESCRIBE statement, then will return the resource identifier, the other statements will return true;
Having said so much, the context of the message board has come out.
Start with the code below
conn.php
<span style= "Font-family:comic Sans ms;font-size:14px;" ><?php include ("head.php"); $dbName = "BBS"; $conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link Error"); $flag = mysql_select_db ($dbName, $conn); mysql_query ("Set names ' GBK '"); Use GBK Chinese code; function Tohtmlcode ($content) { return $content = Str_replace ("\ n", "<br>", Str_replace ("", " " , $content)); } ? ></span>
There's a Tohtmlcode custom function that replaces the return (\ n) in the string with the newline <br> in the HTML, replacing the space with spaces in the HTML ( )
One of the functions is described below
Grammar
Str_replace (Find,replace,string,count)
addmsg.php
<span style= "Font-family:comic Sans ms;font-size:14px;" ><?php//Referencing the connection database file ("conn.php") that was written before; if (@$_post[' submit ']) {$sql = "INSERT into message (id,user,title,content,lastdate)". "Values (' ', ' $_post[username] ', ' $_post[title] ', ' $_post[content] ', now ())"; mysql_query ($sql); echo "Add success"; }?> <script language=javascript> function checkpost () {if (myform.username.value== "") { Alert ("Please fill in User name"); Myform.user.focus (); return false; } if (myform.title.value.length<5) {alert ("title cannot be less than 5 characters"); Myform.title.focus (); return false; } if (myform.content.value== "") {alert ("must fill in message content"); Myform.content.focus (); return false; }} </SCRIPT> <form action= "addmsg.php" method= "POST" name = "MyForm" onsubmit= "return Checkpost ();" > Name: <input type= "text" size= "ten" name= "UserName"/><br/> Title: <input type= "text" name= "title"/><br/> Content: <textarea name= "Content" cols= "rows=" 9 " ></textarea><br/> <input type= "Submit" name= "Submit" value= "submission message"/> </form> </ Span>
Include is the introduction of conn.php, similar to the C language include
The $_post variable is an array that collects the values from the form method= "POST", the key that the post emits
The value pair is stored in this $_post array $_post[' submit ' key to the value of the submit, if the trigger, that is, checkpost return to True, the POST value,
Obviously $_post[' submit ' is not empty and non-empty is true, then execute the INSERT statement inside IF. Keep the message content in the MySQL database.
listmsg.php
<span style= "Font-family:comic Sans ms;font-size:14px;" ><?php include ("conn.php"); ? > <table width=500 border= "0" align= "center" cellpadding= "5" cellspacing= "1" bgcolor= "#add3ef" > <?php $sql = "SELECT * from Message ORDER by lastdate DESC"; $query = mysql_query ($sql); while ($row = Mysql_fetch_array ($query)) { ?> <tr bgcolor= "#eff3ff" > <td><b>< Big> title: <?= $row [' title ']?></big><b/> <b><sub> User: <?= $row [' User ']?></sub></b></td> </tr> <tr bgcolor= "#ffffff" > <td> Content: <?= tohtmlcode ($row [' content '])?></td> </tr> <?php } ?> </table> </span>
PHP and HTML code mixed seems to be quite messy.
PHP gets the message from MySQL and displays it on the page, which I'll show in the table. The main code is above.