PHP combined with MySQL database to realize the function _php instance of the message board

Source: Internet
Author: User

First show you the Message board effect chart:


Recently looked at the basic PHP syntax, I want to use these basic things to realize the message board, but also a consolidation of basic knowledge.

What is a message board? A carrier that can be used to record and display text messages.

Now cut to the point, said the message board is how to achieve!

First, after the user submitted a message, the relevant content into the server, when he wanted to see the background and then all the messages read out, and finally displayed in the browser, users can see the message.

One of the tools in the background that needs to be easy to read and write data, I choose the MySQL database to help me accomplish these things.

I wrote the main three php files, respectively:

conn.php Connection database;

addmsg.php php from the page to read the message related content, and put it into the (Insert) database;

listmsg.php reads the message content from the database, then displays it on the page;

1. Prepare to build the database table structure , below is my table structure under the phpMyAdmin screenshot:


Building table Syntax

SQL CREATE TABLE Syntax
CREATE table name
(
column name 1 data type,
column name 2 data type,
column name 3 data type,
...
)

2.php to connect the MySQL database, and then select one of the databases, I chose here is the BBS database (PS created before) Here are a few to use the PHP library functions,

Copy Code code as follows:

①mysql_connect ("localhost", "root", "")

PHP connection MySQL, the parameters are MySQL address (localhost representative Ben), username, password

Return value: Returns a connection identifier successfully if the connection fails to return false

Copy Code code as follows:

②mysql_select_db ($dbName, $conn);

MySQL can have a lot of db, so you need to select one of the DB for the next action.

Parameters: The first is the database name, the second is the link identifier, you can put the return value in the ① here, representing that I will use the ① in MySQL.

Return value: False connection failed, true connection succeeded.

Copy Code code as follows:

③mysql_query (query,connection)

Parameters: Query represents the statement you want MySQL to execute

Connection optional, SQL connection identifier as described above

return value: mysql_query () returns a resource identifier only for Select,show,explain or describe statements and FALSE if the query executes incorrectly.

For other types of SQL statements, mysql_query () returns TRUE on successful execution, and returns FALSE when an error occurs.

Personal summary of this return value: This function fails to return false, execution success depends on what statement, if it is select,show,explain or DESCRIBE statement, then returns the resource identifier, the other statement returns true;

Having said so much, the thread of the message board has already come out.

Start 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 )); 
} 

There is a Tohtmlcode custom function that replaces a carriage return (\ n) in the string with a newline <br> in HTML, and replaces a space with a space in the HTML ()
One of the functions is described below

Grammar

Copy Code code as follows:

Str_replace (Find,replace,string,count)

Parameters Description
Find Necessary. Specify the value to find.
Replace Necessary. Specify a value that replaces the value in Find.
String Necessary. Specify the string to be searched for.
Count Optional. A variable that counts the number of replacements.

addmsg.php

<span style= "Font-family:comic Sans ms;font-size:14px;" 
><?php//reference the previously written connection database file include ("conn.php"); 
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 the 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"/>&lt Content: <textarea name= "Content" cols= ";br/>" rows= "9" ></textarea><br/> 
<input type= "Submit" name= "Submission" value= "submitting 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 from the method= "post", and the value of the Post's key value to the $_post[' submit ' in this $_post array, and if it triggers a submit, That is, when Checkpost returns True, it will POST the value, obviously $_post[' submit ' is not NULL, Non-empty is true, then execute the INSERT statement inside the 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 > 

The mix of PHP and HTML code looks pretty messy.

PHP gets the message content from MySQL and displays it on the page, which I show here in the table. The main code is above these.

The above is a small set of PHP to share with the MySQL database to achieve the function of the message board, I hope to help!

Related Article

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.