search
HomeBackend DevelopmentPHP TutorialPHP combines Mysql database to implement message board function, mysql message board_PHP tutorial


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 数据类型,
....
)

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", "")

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

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)

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>

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)
参数 描述
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>

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>

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

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

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),