好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP+MYSQL留言本(二) - 综合实例

PHP+MYSQL留言本(二)

昨天已经把留言本的大概功能弄了出来`~~~今天再稍微改善下加一个管理员管理留言的功能~~~这里最主要要用到 $_session['item'] 这个东东~~`

好了`~`先把昨天的稍微改一下 再把这个功能加进去~~~

首先我们在首页同时显示留言,以及留言添加框~~这样使留言者方便使用`~`

不说多了`~把代码贴出来再说:

index.php

<html>   <head>   <meta http-equiv= "Content-Type"  content= "text/html; charset=gb2312" >   <title>留言本</title>   </head>    <body>   <a href= "admin_login.htm"  tagert= "_blank" >留言管理</a>      <?         $conn =MySQL_connect ( "localhost:6033" ,  "root" ,  "" );  //打开MySQL服务器连接        mysql_select_db( "guest_book" );  //链接数据库        mysql_query( "set names GB2312" );  //解决中文乱码问题         $exec = "select * from contents" ;  //sql语句         $result =mysql_query( $exec );  //执行sql语句,返回结果         while ( $rs =mysql_fetch_object( $result ))        {          echo   "<table><tr><td>姓名:" . $rs ->name. "</td></tr>" ;          echo   "<tr><td>留言:" . $rs ->content. "</td></tr></table><br/>" ;          echo   "............................................................................................................................." ;       }          mysql_close();    ?>   <br><br><br>   <form action= "updata.php"  method= "post"  name= "name1" >       姓名:<input type= "text"  name= "user_name" ><br>       留言:<textarea name= "post_contents"  rows= "10"  cols= "50" ></textarea>       <input type= "submit"  value= "提交留言" >      </form>   </body>   </html> 

updata.php页再加个header("location:index.php");语句重定向到主页面`~~

updata.php

<?      $name = $_POST [ 'user_name' ];      $content = $_POST [ 'post_contents' ];      $conn =mysql_connect( "localhost:6033" ,  "root" ,  "" );      mysql_query( "set names GB2312" );  //解决中文乱码问题      mysql_select_db( "guest_book" );       $exec = "insert into contents (name,content) values ('" . $_POST ['user_name ']."' , '".$_POST[' post_contents ']."' )";       $result =mysql_query( $exec );      mysql_close();      header( "location:index.php" );   ?> 

HOHO~~~是不是用起来有那么回事了`~~

好的`~下面再加个管理功能 ~~那么这个留言本就更加强大了`~

留言管理模块 分为 管理员登录页admin_login.htm ,管理员验证页admin_check.php  后台管理首页admin_index.php

先弄这个登录页面admin_login.htm

< form   action = "admin_check.php"   method = "post"   name = "form2" >      用户名: < input   type = "text"   name = "admin_name" >      密  码: < input   type = "passWord"   name = "admin_password" >      < input   type = "submit"   value = "进入后台管理" >   </ form >  

这个简单得再简单不过了,我就不说什么了`~~

admin_check.php管理员验证

<?     session_start();      $admin_name = $_POST [ 'admin_name' ];      $admin_password = $_POST [ 'admin_password' ];      $conn =mysql_connect ( "localhost:6033" ,  "root" ,  "" );     mysql_select_db( "guest_book" );      $exec = "select * from admin where admin_name='" . $admin_name . "'" ;      $result =mysql_query( $exec );       if  ( $rs =mysql_fetch_object( $result ))       {  if  ( $rs ->admin_password== $admin_password )          { $_SESSION [ 'admin' ]= "OK" ;           header( "location:admin_index.php" );           }          else   echo "密码不正确" ;       }       else   echo "用户名不正确" ;           mysql_close();   ?> 

这里最主要的就是session~~~凡事要用到session的地方.在页面最开始处要加上这一句session_start();否则就无法使用~~那么session究竟是什么东东呢?由于网页的传输方式(也就是http这个东西) 不是永久连接的~~`所以服务器无法在两个不同页面之间传送变量`~~唉.我一下子也说不清楚`~~还是看看这里 http://HdhCmsTestphpfensi测试数据/php/phphuihua/ 上面有很详细的介绍.反正就是用这个东西来验证管理员的身分了`~~

好了下面说后台管理主页面admin_index.php

<?   session_start();   if ( $_SESSION [ 'admin' ]== "OK" )    {      $conn =mysql_connect ( "localhost:6033" ,  "root" ,  "" );     mysql_select_db( "guest_book" );      $exec = "select * from contents" ;      $result =mysql_query( $exec );      while ( $rs =mysql_fetch_object( $result ))        {          echo   "<table><tr><td>姓名:" . $rs ->name. "</td></tr>" ;          echo   "<tr><td>留言:" . $rs ->content. "</td></tr></table><br/>" ;          echo    "<a href=modify.php?id=" . $rs ->id. " >修改</a>      <a href=delete.php?id=" . $rs ->id. " >删除</a>" ;        }     echo   "<br><br><br><br><br><a href=index.php >回首页</a>" ;    }    mysql_close();   ?> 

这里最主要是这一句echo  "<a href=modify.php?id=".$rs->id." >修改</a><a href=delete.php?id=".$rs->id." >删除</a>";

用来向所连接到的地址传递参数~~看看下面的就知道有什么用了

modify.php

<?    session_start();     if ( $_SESSION [ 'admin' ]== "OK" )    {      $conn =mysql_connect ( "localhost:6033" ,  "root" ,  "" );     mysql_select_db( "guest_book" );      $exec = "select * from contents where id=" . $_GET [ 'id' ];   /*这里这个$_GET['id']就是取得从那个连接传递过来的参数拉 */      $result =mysql_query( $exec );      $rs =mysql_fetch_object( $result );      $name = $rs ->name;      $content = $rs ->content;      $id = $rs ->id;    ?>       <form action= "modify2.php"  method= "post"  name= "name1" >       ID  :<?= $id ?><input type=hidden name=id value=<?= $id ?> >       姓名:<?= $name ?><br>       留言:<textarea name= "post_contents"  rows= "10"  cols= "50" ><?= $content ?></textarea>       <input type= "submit"  value= "提交修改" >      </form>    <?     }   mysql_close();   ?> 

这里这个<?=$id> 其实就等于 echo $id

再看看最终的数据修改实现页面modify2.php

<?    session_start();     if ( $_SESSION [ 'admin' ]== "OK" )    {      $conn =mysql_connect ( "localhost:6033" ,  "root" ,  "" );     mysql_select_db( "guest_book" );      $exec = "select * from contents where id=" . $_GET [ 'id' ];      $exec = "update contents set content='" . $_POST ['post_contents ']."'  where id=". $_POST [ 'id' ];      $result =mysql_query( $exec );        }    mysql_close();    header( "location:admin_index.php" );   ?> 

最后就是删除功能的实现了

delete.php

<?    session_start();     if ( $_SESSION [ 'admin' ]== "OK" )    {      $conn =mysql_connect ( "localhost:6033" ,  "root" ,  "" );     mysql_select_db( "guest_book" );      $exec = "delete from contents where id=" . $_GET [ 'id' ];     mysql_query( $exec );     mysql_close();    header( "location:admin_index.php" );    }   ?> 

今天用到的知识如下:

1: session_start();  $_SESSION['变量名']=$变量名 或者 某一特定值

2: <a href="#####.php?var=##">aaa</a>用这个方法来传递参数  同时用 $_GET['var']来接收传递过来的值

3: 数据修改 :$exec="update tablename set item1='".$_POST['item1']."' where ...";

4: 数据删除 :$exec="delete from tablename where...";

是不是看起来一团\乱麻~~~~明天用include 还有requre 好好修改下`~~~~让程序看起来清晰点~~~

查看更多关于PHP+MYSQL留言本(二) - 综合实例的详细内容...

  阅读:39次