解决方案 »

  1.   

    这是很基础的东西。
    需要用到数据库,可以查看mysql 的dbconnect,select,insert,update,delete语法。
    写了个demo,可以插入数据库,从数据库中按时间倒序显示记录,希望对你有帮助。dbname是 demo连接数据库
    $conn=@mysql_connect("localhost","root","")  or die(mysql_error());
    @mysql_select_db('demo',$conn) or die(mysql_error());localhost 是服务器ip,本机用localhost
    root是数据库用户名
    密码为空。db结构CREATE TABLE `test` (
      `id` int(10) unsigned NOT NULL auto_increment,
      `name` varchar(100) NOT NULL,
      `age` tinyint(4) unsigned NOT NULL,
      `addtime` datetime NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    db.php 用于显示记录<?php //打开数据库
    function opendb(){
    $conn=@mysql_connect("localhost","root","")  or die(mysql_error());
    @mysql_select_db('demo',$conn) or die(mysql_error());
    } //关闭数据库
    function closedb(){
    @mysql_close() or die("關閉數據庫出錯!");
    }    opendb();    echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';    $sqlstr = "select * from test order by addtime desc";
        $query = mysql_query($sqlstr) or die(mysql_error());    while($thread=mysql_fetch_assoc($query)){
            $result[] = $thread;
        }    if($result){
            foreach($result as $val){
                echo $val['id'].' '.$val['name'].' '.$val['age'].' '.$val['addtime'].'<br>';
            }
        }?>
    add.php 用于新增记录<?php//打开数据库
    function opendb(){
        $conn=@mysql_connect("localhost","root","")  or die(mysql_error());
        @mysql_select_db('demo',$conn) or die(mysql_error());
    }//关闭数据库
    function closedb(){
        @mysql_close() or die("關閉數據庫出錯!");
    }opendb();
    $send = isset($_POST['send'])? $_POST['send'] : '';if($send=='true'){ // submit    $name = isset($_POST['name'])? $_POST['name'] : ''; 
        $age = isset($_POST['age'])? $_POST['age'] : '';
        $addtime = date('Y-m-d H:i:s');    if($name=='' || $age==''){
            exit('name or age is empty');
        }    $sqlstr = "insert into test(name,age,addtime) values('{$name}','{$age}','{$addtime}')";
        mysql_query($sqlstr) or die(mysql_error());    echo 'insert success';}else{?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <meta http-equiv="conent" content="text/html;charset=utf-8">
      <title> New Document </title>
      <meta name="Generator" content="EditPlus">
     </head> <body>
      <form name="form1" method="post" action="add.php">
      <p>name:<input type="text" name="name"></p>
      <p>age:<input type="text" name="age"></p>
      <p><input type="submit" value="submit"></p>
      <input type="hidden" name="send" value="true">
      </form>
     </body>
    </html>
    <? } ?>