1、创建数据库
CREATE TABLE news( id INT( 12 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , title VARCHAR( 50 ) , dates DATE, connects TEXT, hits INT( 11 ) NOT NULL ) ENGINE = INNODB DEFAULT CHARSET = utf8 AUTO_INCREMENT =1
2、连接数据库conn.php
<?php @mysql_connect("127.0.0.2","root","")or die("mysql connect false"); @mysql_select_db("wikidb")or die("database connect false"); //mysql_set_charset("utf-8"); mysql_query("set names ‘utf-8‘"); ?>
3、建立主页index.php
<a href=‘add.php‘>添加内容</a><hr> <form action="index.php" method="get"> <input type="text" name="keys"> <input type="submit" name="subs" value="search"> </form> <?php include("conn.php");//conn datebase; if(!empty($_GET[‘keys‘])){ $w="`title` like ‘%".$_GET[‘keys‘]."%‘"; }else{ $w=1; } $sql="SELECT * FROM `news` where $w order by id desc limit 10"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ ?> <h2>标题:<a href="view.php?id=<?php echo $rs[‘id‘]?>"><?php echo $rs[‘title‘]?></a> |<a href="edit.php?id=<?php echo $rs[‘id‘]?>">编辑</a>|<a href="del.php?del=<?php echo $rs[‘id‘]?>">删除</a>|</h2> <li><?php echo $rs[‘dates‘]?></li> <p><?php echo iconv_substr($rs[‘connects‘],0,100,"")?><a href="view.php?id=<?php echo $rs[‘id‘]?>">[继续阅读]</a></p> <hr> <?php } ?>
4、建立内容页view.php
<?php include("conn.php");//conn datebase; if(!empty($_GET[‘id‘])){ $sql="SELECT * FROM `news` WHERE `id`=‘".$_GET[‘id‘]."‘"; $query=mysql_query($sql); $rs=mysql_fetch_array($query); $sqlup="UPDATE `news` SET `hits`=hits+1 WHERE `id`=‘".$_GET[‘id‘]."‘"; mysql_query($sqlup); } ?> <h1><?php echo $rs[‘title‘]?></h1> <h2><?php echo $rs[‘dates‘]?></h2> <h3>点击量:<?php echo $rs[‘hits‘]?></h3><hr> <p><?php echo $rs[‘connects‘]?></p>
5、建立编辑页edit.php
<?php include("conn.php");//conn datebase; if(!empty($_GET[‘id‘])){ $sql="SELECT * FROM `news` WHERE `id`=‘".$_GET[‘id‘]."‘"; $query=mysql_query($sql); $rs=mysql_fetch_array($query); } if(!empty($_POST[‘sub‘])){ $title=$_POST[‘title‘]; $con=$_POST[‘con‘]; $hid=$_POST[‘hid‘]; $sql="UPDATE `news` SET `title`=‘$title‘,`connects`=‘$con‘ WHERE id=‘$hid‘ limit 1"; mysql_query($sql); echo "<script>alert(‘update successful!‘);location.href=‘index.php‘</script>"; } ?> <form action="edit.php" method="post"> <input type="hidden" name="hid" value="<?php echo $rs[‘id‘] ?>"> 标题<input type="text" name="title" value="<?php echo $rs[‘title‘] ?>"><br> 内容<textarea rows="5" cols="50" name="con"><?php echo $rs[‘connects‘] ?></textarea><br> <input type="submit" name="sub" value="发表"> </form>
6、建立删除页del.php
<?php include("conn.php");//conn datebase; if(!empty($_GET[‘del‘])){ $d=$_GET[‘del‘]; $sql="DELETE FROM `news` WHERE `id`=‘$d‘"; mysql_query($sql); echo "delete successful"; } ?>
PHP+MYSQL微型博客系统
时间: 2024-09-30 21:57:52