PHP+Mysql 实现留言板的功能
最近看了下PHP基础语法,就想利用这些基本东西实现留言板,也是对基础知识的一个巩固。
什么是留言板?一种可以用来记录,展示文字信息的载体。
现切入正题,说说本次留言板是怎么实现!
首先用户提交留言后,相关内容存入服务器,当他想看的时候后台再把所有留言读出来,最后显示在浏览器上,用户就可以看到留言了。
这其中后台需要便于读写数据的一个工具,我选择MySQL数据库来帮助我完成这些事。
我写了主要是三个php文件,分别是:
conn.php 连接数据库;
addmsg.php php从页面读取留言相关内容,并且把它存入(Insert)数据库;
listmsg.php 从数据库中读取留言内容,然后把它显示在页面上;
1.准备建立数据库表的结构,下面是我的表结构在phpMyAdmin下的截图:
建表语法
SQL CREATE TABLE 语法
CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据类型, 列名称3 数据类型, .... )
2.php连接mysql数据库,然后选择其中一个数据库,我这里选的是bbs数据库(ps 之前创建的) 下面介绍几个要用到的php库函数,
①mysql_connect("localhost", "root", "")
php连接mysql,参数分别是mysql地址(localhost代表本机),用户名,密码
返回值:如果连接失败返回false,成功返回一个连接标识符
②mysql_select_db($dbName, $conn);
mysql里可以有很多db,所以你需要选择一个其中一个db进行接下来的操作。
参数:第一个是数据库名称,第二个是链接标识符,可以把①中的返回值放这里,代表的是我将使用①中的mysql。
返回值:false 连接失败,true连接成功。
③mysql_query(query,connection)
参数:query代表你要mysql执行的语句
connection 可选,SQL连接标识符同上面所讲
返回值:mysql_query() 仅对 SELECT,SHOW,EXPLAIN或DESCRIBE语句返回一个资源标识符,如果查询执行不正确则返回 FALSE。
对于其它类型的 SQL 语句,mysql_query() 在执行成功时返回 TRUE,出错时返回 FALSE。
个人对这个返回值的总结:此函数执行失败就返回false;执行成功要看是什么语句,如果是SELECT,SHOW,EXPLAIN 或 DESCRIBE 语句,那么就会返回资源标识符,其他的语句就返回true ;
说了这么多, 留言板的脉络已经出来了
下面开始上代码
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 'GBK'"); //使用GBK中文编码; function toHtmlcode($content) { return $content = str_replace("\n","<br>",str_replace(" ", " ", $content)); } ?></span>
上面有一个toHtmlcode自定义函数功能是把字符串中回车(\n)替换成html中的换行
,把空格替换成html中的空格( )
其中有一个函数介绍如下
语法
str_replace(find,replace,string,count)
addmsg.php
<span style="font-family:Comic Sans MS;font-size:14px;"><?php // 引用之前写好的连接数据库文件 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 "添加成功"; } ?> <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['title']?></big><b/> <b><sub> 用户:<?= $row['user']?></sub></b></td> </tr> <tr bgColor="#ffffff"> <td>内容:<?= toHtmlcode($row['content'])?></td> </tr> <?php } ?> </table> </span>
php与html代码混编看起来还是比较乱的。
php从mysql中获取留言内容,并把它显示在页面上,我这里显示在table里。主要代码就上面这些。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
