php之留言板
留言板或者说一个小的博客系统有如下功能,编写标题内容并用mySQL保存,修改,删除。
1 <?php 2 @mysql_connect("localhost:3306", "root", "") or die("mysql连接失败"); 3 @mysql_select_db("php100") or die("db连接失败"); 4 //mysql_set_charset("gbk"); 5.2.3以上可以这样写,纠正函数编码 5 mysql_query("set names 'gb2312_chinese_ci'"); // 6 mysql_query("SET NAMES 'UTF8'"); //以此来保证网页传送中文汉字进入数据库时不会变成乱码 7 ?>
1 <a href='add.php'>添加内容</a> 2 <hr> 3 <?php 4 header("Content-type: text/html; charset=utf-8");//保证正常显示中文 5 include_once ("conn.php"); 6 7 $sql = "select * from `news` order by id desc"; 8 $query = mysql_query($sql); 9 10 while( $rs = mysql_fetch_array($query)) {//执行一次向下读取一条数据 11 12 ?> 13 <h2>标题:<?php echo $rs["title"] ?> | 14 <a href="edit.php?ed=<?php echo $rs['id']?>">编辑</a> | 15 <a href="del.php?del=<?php echo $rs['id']?>">删除</a> |</h2> 16 <li>时间:<?php echo $rs["dates"] ?></li> 17 <p><?php echo $rs["contents"] ?></p> 18 <hr> 19 <?php//典型的混编,简直醉了 20 } 21 ?>
1 <?php//添加内容页面 2 header("Content-type: text/html; charset=utf-8"); 3 include ("conn.php");//引入连接数据库 4 5 if(!empty($_POST['sub'])){ 6 $title = $_POST['title']; 7 $con = $_POST['con']; 8 /*$sql = "CREATE TABLE news //这个是建立表的语句 9 ( 10 id INT(5) NOT NULL AUTO_INCREMENT, 11 PRIMARY KEY(id), 12 title VARCHAR(50), 13 dates DATE, 14 contents TEXT 15 )";*/ 16 $sql="insert into `news` (`id`,`title`,`dates`, `contents`) value (null, '$title',now(),'$con')"; 17 18 echo $sql."<br/>"; 19 mysql_query($sql); 20 echo "insert is ok"; 21 } 22 23 24 ?> 25 26 <form action="add.php" method="post"> 27 标题<input type="text" name="title"><br> 28 内容<textarea rows="5" cols="50" name="con"></textarea><br> 29 <input type="submit" name="sub" value="发表"> 30 </form>
1 <?php 2 3 include ("conn.php"); 4 header("Content-type: text/html; charset=utf-8"); 5 6 if(!empty($_GET['del'])) {//在index页面中,连接后面接着的?del='$id'可以用get方法获得,以此来简单的传递信息 7 $d = $_GET['del']; 8 $sql = "delete from `news` where `id` = '$d'"; 9 mysql_query($sql); 10 echo "删除成功"; 11 } 12 ?>
1 <?php 2 include_once("conn.php"); 3 header("Content-type: text/html; charset=utf-8"); 4 if(!empty($_GET['ed'])){//同样用链接的后缀传递id 5 $ed = $_GET['ed']; 6 $sql = "select * from `news` where `id`='$ed'"; 7 $query = mysql_query($sql); 8 $rs = mysql_fetch_array($query); 9 ?> 10 11 <h2>标题:<?php echo $rs['title']?></h2> 12 <li>时间:<?php echo $rs['dates']?></li> 13 <form method="POST" action="edit.php"> 14 <input type="hidden" name="hid" value="<?php echo $ed?>"/>//隐藏的id来传递,用post方法,这种写法也学会,并不会在页面显示 15 内容:<textarea name="con" rows="5" cols="40"><?php echo $rs['contents']?></textarea> 16 <br><br> 17 <input type="submit" name="submit" value="修改"/> 18 </form> 19 20 <?php 21 } 22 23 if(!empty($_POST['submit'])) { 24 $content = $_POST['con']; 25 $hid = $_POST['hid']; 26 $sql = "update `news` set `contents`='$content' where `id`='$hid'"; 27 mysql_query($sql); 28 echo "<script>alert('更新成功');location.href='index.php'</script>"; 29 } 30 31 ?>
posted on 2014-08-07 22:15 Zachary_wiz 阅读(182) 评论(0) 编辑 收藏 举报