Table of Contents
PHP combines with Mysql database to implement the message board function. Mysql message board
Home Backend Development PHP Tutorial PHP combines Mysql database to implement message board function, mysql message board_PHP tutorial

PHP combines Mysql database to implement message board function, mysql message board_PHP tutorial

Jun 20, 2018 am 09:23 AM
mysql mysql database php message board


PHP combines with Mysql database to implement the message board function. Mysql message board

First, I will show you the message board renderings:


I recently looked at the basic syntax of PHP and wanted to use these basic things to implement a message board, which is also a consolidation of basic knowledge.

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

Now let’s get to the point and talk about how this message board was implemented!

First, after the user submits a message, the relevant content is stored in the server. When he wants to read it, all the messages are read out in the background, and finally displayed on the browser, and the user can see the message.

The backend needs a tool that facilitates reading and writing data. I chose the mysql database to help me complete these things.

I wrote mainly three php files, namely:

conn.php Connect to the database;

addmsg.php php reads message-related content from the page and stores it (Insert) into the database;

listmsg.php reads from the database Leave the message content and then display it on the page;

1. Prepare to establish the structure of the database table . Here is a screenshot of my table structure under phpMyAdmin:


Table creation syntax

SQL CREATE TABLE 语法
CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
)
Copy after login

2.php to connect to mysql Database, and then select one of the databases . I chose the bbs database (created before ps). Here are some php library functions to be used,

①mysql_connect("localhost", "root", "")
Copy after login

php connection mysql, the parameters are mysql address (localhost represents the local machine), user name, password

Return value: If the connection fails, return false, if successful, return a connection identifier

②mysql_select_db($dbName, $conn);
Copy after login

There can be many dbs in mysql, so you need to select one of them 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 the mysql in ①.

Return value: false if the connection fails, true if the connection is successful.

③mysql_query(query,connection)
Copy after login

Parameter: query represents the statement you want mysql to execute

connection optional, the SQL connection identifier is the same as mentioned above

return Value: mysql_query() returns a resource identifier only for SELECT, SHOW, EXPLAIN, or DESCRIBE statements, or FALSE if the query was executed incorrectly.

For other types of SQL statements, mysql_query() returns TRUE when executed successfully and FALSE when an error occurs.

Personal summary of this return value: If this function fails to execute, it returns false; if it is executed successfully, it depends on what statement it is. If it is a SELECT, SHOW, EXPLAIN or DESCRIBE statement, then the resource identifier will be returned. symbol, other statements will return true;

Having said so much, the context of the message board has come out

Let’s start the code

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("数据库链接错误"); 
$flag = mysql_select_db($dbName, $conn); 
mysql_query("set names &#39;GBK&#39;"); //使用GBK中文编码; 
function toHtmlcode($content) 
{ 
return $content = str_replace("\n","<br>",str_replace(" ", " ", $content)); 
} 
?></span>
Copy after login

There is a toHtmlcode custom function above, which is to replace the carriage return (n) in the string with the line feed in html
, and replace the spaces with spaces in html ( )
One of the functions is introduced as follows

Syntax

str_replace(find,replace,string,count)
Copy after login
参数描述
find必需。规定要查找的值。
replace必需。规定替换 find 中的值的值。
string必需。规定被搜索的字符串。
count可选。一个变量,对替换数进行计数。

addmsg.php

<span style="font-family:Comic Sans MS;font-size:14px;"><?php 
// 引用之前写好的连接数据库文件 
include("conn.php"); 
if(@$_POST[&#39;submit&#39;]){ 
$sql = "insert into message (id,user,title,content,lastdate)" . 
"values ( &#39;&#39;,&#39;$_POST[userName]&#39;,&#39;$_POST[title]&#39;,&#39;$_POST[content]&#39;,now())"; 
mysql_query($sql); 
echo "添加成功"; 
} 
?> 
<SCRIPT language=javascript> 
function CheckPost() 
{ 
if (myform.userName.value=="") 
{ 
alert("请填写用户名"); 
myform.user.focus(); 
return false; 
} 
if (myform.title.value.length<5) 
{ 
alert("标题不能少于5个字符"); 
myform.title.focus(); 
return false; 
} 
if (myform.content.value=="") 
{ 
alert("必须要填写留言内容"); 
myform.content.focus(); 
return false; 
} 
} 
</SCRIPT> 
<form action="addmsg.php" method="post" name = "myform" onsubmit="return CheckPost();"> 
用名:<input type="text" size="10" name="userName" /><br/> 
标题:<input type="text" name="title" /><br/> 
内容:<textarea name="content" cols="60" rows="9" ></textarea><br/> 
<input type="submit" name="submit" value="提交留言" /> 
</form> 
</span>
Copy after login

include 是引入conn.php,类似于c语言中include

$_POST 变量是一个数组,此变量用于收集来自 method="post" 的表单中的值,post发出的键值对存于此$_POST数组中$_POST['submit'] 取键submit的值,如果触发submit,也就是CheckPost返回为true时,会post值,显然$_POST['submit']不为空,非空即为真,那么就执行if里面的插入语句。使留言内容保存在mysql数据库中。

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> 
标题:<?= $row[&#39;title&#39;]?></big><b/> <b><sub> 
用户:<?= $row[&#39;user&#39;]?></sub></b></td> 
</tr> 
<tr bgColor="#ffffff"> 
<td>内容:<?= toHtmlcode($row[&#39;content&#39;])?></td> 
</tr> 
<?php 
} 
?> 
</table> 
</span>
Copy after login

php与html代码混编看起来还是比较乱的。

php从mysql中获取留言内容,并把它显示在页面上,我这里显示在table里。主要代码就上面这些。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles