Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411349
  • 博文数量: 48
  • 博客积分: 1032
  • 博客等级: 上士
  • 技术积分: 1256
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-19 13:24
文章分类

全部博文(48)

文章存档

2014年(3)

2013年(23)

2012年(22)

分类: Web开发

2013-03-05 00:10:11

HTML部分:


点击(此处)折叠或打开

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>留言板项目</title>
  6. </head>

  7. <body>
  8. <form action="pub.php" method="post">
  9.     <p>留言标题:<input type="text" name="title" /></p>
  10.     <p>留言者:<input type="text" name="usrname" /></p>
  11.     <p>留言内容:<br />
  12.     <textarea name="content" cols="10" rows="10"></textarea>
  13.     </p>
  14.     <p>
  15.     <input type="submit" value="提交留言" />
  16.     </p>
  17.     
  18. </form>
  19. </body>
  20. </html>
将数据保存到数据库,将中的问号和p中间不能有空格;



点击(此处)折叠或打开

  1. <?php

  2. //获取环境变量值
  3. $title = $_POST['title'];

  4. $usrname = $_POST['usrname'];

  5. $content = $_POST['content'];

  6. //链接数据库

  7. $conn = mysql_connect('localhost','root','111111');

  8. $sql = 'use php';

  9. mysql_query($sql,$conn);

  10. $sql = "insert into msg (title,name,content) values ('$title','$usrname','$content')";

  11. $rs = mysql_query($sql,$conn);

  12. if($rs){
  13.     echo '留言成功';
  14. }
  15. else{
  16.     echo '留言失败';
  17.     echo mysql_error();//该函数获取失败错误代码;
  18. }


  19. ?>
查看数据库内容



点击(此处)折叠或打开

  1. <?php

  2. $conn = mysql_connect('localhost','root','111111');

  3. $sql = 'use php';

  4. mysql_query($sql,$conn);

  5. $sql = 'select * from msg';

  6. $rs = mysql_query($sql,$conn);

  7. //var_dump($rs);

  8. //拿数据

  9. //var_dump(mysql_fetch_assoc($rs));

  10. $list = array();

  11. while($row = mysql_fetch_assoc($rs)){
  12.     $list[] = $row; //将$row保存到数组$list数组中
  13. }

  14. print_r($list);

  15. //查看数据库内容
  16. /*
  17.     $id = $_GET['id'] + 0;//转换为整型;
  18.     
  19.     if($id == 0){
  20.         echo '参数有误';
  21.         exit;
  22.     }
  23.     
  24.     $conn = mysql_connect('localhost','root','111111');
  25.     
  26.     $sql = 'use php';
  27.     
  28.     mysql_query($sql,$conn);
  29.     
  30.     $sql = "select * from msg where id = $id";
  31.     
  32.     $rs = mysql_query($sql,$conn);
  33.     
  34.     $msg = mysql_fetch_assoc($rs);
  35.     
  36.     if(!$msg){
  37.         echo '参数有误';
  38.         exit;
  39.     }
  40.     
  41.     print_r($msg);
  42.     
  43.     
  44. */


  45. ?>


阅读(2308) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~