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

不言
Release: 2023-02-28 17:42:01
Original
2374 people have browsed it


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里。主要代码就上面这些。

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!