用PHP+MYSQL写了一个简单的留言本~~体验到了数据库真的很好用,比文件好用很多,至少不需要设置它怎么写进去,怎么读出来的条件.

mysql中的执行命令:
use test;
create table user ( //创建一张表,名字叫user      
       id int(4) not null auto_increment,//为整型的id,不允许为空,且自动增加
       title varchar(100),
       name varchar(20),
       content varchar(1000),
       primary key(id));//主键

html代码:
<html>
<head><title>welcome</title></head>
<body>
<form action="send.php">
  标题:<input type="text" name="title" size=20><br>
发表人:<input type="text" name="name" size=20><br>
  内容: <textarea name="content" cols=80 rows=15></textarea><br>
        <input type="submit" value="发送">
        <input type="reset" value="重写">
</body>
</html>
php代码

head.php
<?php
  $conn=mysql_connect("localhost","用户名","密码")
               or die("不能连接数据库服务器:".mysql_error());
  mysql_select_db("test",$conn) or die("不能选择数据库".mysql_error());
?>

send.php
<?php
    require("head.php");
    //引用head.php文件,其实和C中的include一样的,php中也有include
    $insertsql="insert into user(title,name,content) values('$title','$name','$content')";
     //利用SQL语句执行插入一个新的数据,包括标题,名字和内容
    $insert=mysql_query($insertsql,$conn);
    if($insert){header("location:show.php");}
    else{echo "失败"; exit;}
?>

show.php
<?php
    require("head.php");
    $result=mysql_query("select *from user",$conn);
    while($row=mysql_fetch_row($result))//用循环来输出数据
    {
     print "第".$row[0]."篇";echo"<br>";
     print "标题:".$row[1];echo"<br>";
     print "姓名:".$row[2];echo"<br>";
     print "内容:".$row[3];echo"<br>";
    }
    echo"<br>";
?>

分析一下,首先在MYSQL中建立一个数据库,名字叫text,在text数据库里面建立一张表,叫user。然后再用记事本建立一个网页叫gbook.htm,里面有表格,然后由访客填写,通过发送,把数据送到了send.php文件中处理,然后存进了数据库。要使用程序的时候,把数据从数据库中读取出来,显示在屏幕上。