PHP with MySQL database to implement message board function, MySQL message board _php tutorial

Source: Internet
Author: User
Tags php connect to mysql database

PHP with MySQL database to implement message board function, MySQL message board

First show you the message board:


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 a carriage return (\ n) in a string with a line break in HTML
, replace the space with spaces in HTML ()
One of the functions is described below

Grammar

Str_replace (Find,replace,string,count)
Parameters Description
find
replace
string
Count Optional. A variable that counts the number of replacements.

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"); myfor M.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=" submit 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 value of the key value that the post emits in this $_post array $_post[' submit ' key, and if the submit is triggered, That is, when Checkpost returns to True, the POST value is $_post[, obviously, ' submit ' is not empty and non-null is true, then the INSERT statement inside the IF is executed. 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.

  • 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.