search
Homephp教程PHP源码php与mysql留言板程序实现代码

本程序仅供php+mysql爱好者学习测试用,测试过程中,可能会出现错误,请朋友们根据错误提示修改本程序,本程序为安装版,先运行install目录,然后根据提示操作!

<script>ec(2);</script>


关于PHP语法的那些就不说了,我就说一下连接mysql吧!

 代码如下 复制代码
$conn =@mysql_connect("localhost","root","mydown") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'");

第一句就是描述了连接数据库的语句,并且如果失败的提示”数据库连接错误“;前面的三个参数分别代表了 数据库地址localhost,数据库用户名root,连接数据库密码mydown;

第二句就是描述了连接到数据库表bbs,后面的$conn代表哪个数据库。

第三句话就是转换编码格式,显示中文。

连接好了数据库之后,我们来看下 数据库语句怎么在PHP中提交运行!

 代码如下 复制代码
    $sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);

上面中的蓝色字体大家应该面熟,意思就是 在数据库中插入信息。第二句是在数据库中运行该语句。想起中类似$_POST[title]的变量,大家应该知道,这是通过网页提交获取的信息


实例

install安装

 代码如下 复制代码



留言板安装









* 输入数据库服务器地址:
* 输入数据库服务器用户名:
* 输入数据库服务器密码:
* 输入数据库名称:



check_install.php

 代码如下 复制代码

//获取提交的数据库值
$llcc_server=$_POST['llcc_server'];
$llcc_user_name=$_POST['llcc_user_name'];
$llcc_user_pass=$_POST['llcc_user_pass'];
$llcc_data_name=$_POST['llcc_data_name'];
//打开数据库配置文件
$fp=fopen("../config.php",'w');

if (!$fp) {
    echo "sorry,try again!";
}
//写入文件内容
$outconfig=" $outconfig.='$server=';
$outconfig.="".$llcc_server.";//数据库所在IP,如果是本地,默认localhostn";
$outconfig.='$server_name=';
$outconfig.="".$llcc_user_name.";//连接数据库用户名,一般为rootn";
$outconfig.='$server_pass=';
$outconfig.="".$llcc_user_pass.";//连接数据库密码。n";
$outconfig.='$server_data=';
$outconfig.="".$llcc_data_name.";//数据库名n";
$outconfig.="?>";
$fw=fwrite($fp,$outconfig);

if ($fw) {
    echo "


安装完成!请手动删除目录下install文件夹
返回首页
";
}


?>

login.php

 代码如下 复制代码

include("header.php");
include("conn.php");

  if($_GET[out]){
   setcookie("cookie", "out");
    echo "";
  }


  if($_POST[id]=='admin'){
    $pw=md5($_POST[pw]);
    if($pw=='1b3ae77a2e06580c8cb57e112ddc4a26'){
     setcookie("cookie", "ok");
       echo "";
    }
  }
if($_COOKIE['cookie']!='ok'){
?>




 
 
 
 
 
登录名:
密码:


}else{
?>
 退出

 

   if($_GET[sub]=="清空数据库")
 {
 $sql="delete from message";
 $query=mysql_query($sql);
 }
 ?>
}
?>

conn.php数据库连接

 代码如下 复制代码
include("config.php");
$conn =@mysql_connect($server,$server_name,$server_pass) or die("数据库链接错误");
mysql_select_db($server_data, $conn);//此处代表的要打开的数据库名称,"bbs"是我的数据库名,修改成自己的!
mysql_query("set names 'GBK'"); //使用GBK中文编码;
?>

index.php文件

 代码如下 复制代码

include("conn.php");
include("header.php");
?>
自由屋留言板



$sql="select * from message order by id DESC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
?>
 
 
 
 
 
 
    }
?>
用户名:    标题:   发表时间:
内容:

add.php增加留方程序

 代码如下 复制代码

include("header.php");
include("conn.php");
if($_POST["submit"])
{
$sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "留言已提交";
};
?>








用户:
标题:
内容:
   

test.php数据库结构

 代码如下 复制代码

include("config.php");
include("conn.php");
$sql="CREATE TABLE `message` (
  `id` tinyint(1) NOT NULL auto_increment,
  `user` varchar(25) NOT NULL,
  `title` varchar(50) NOT NULL,
  `content` tinytext NOT NULL,
  `lastdate` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;";
$query=mysql_query($sql);
if($query){
echo"执行成功";}
else echo"执行失败!";
?>

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

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function