您的位置:首页 > 数据库

超微型微博系统(简单实现)

2014-02-12 17:53 274 查看
做出来的效果超丑,但是基本的几个小功能算是实现了,也算我学习的第一个小项目吧!
数据库结构



首先要连接数据库

<?php
@mysql_connect("localhost","root","")or die ("数据库连接不成功");
/*使用mysql_connect函数连接数据库,连接地址为本机,端口默认,用户名:root 密码空,
在前面添加一个@ 是用于屏蔽错误的,后面连接or die 函数。如果数据库连接不成功,则执行or die 语句,显示die 输出。
*/
mysql_select_db("weibo")or die("db连接不成功");
/*
使用mysql_select_db语句连接db,如果连接出错,执行die语句
*/
mysql_query("set names'gbk'");
//设置字符编码格式为utf-8.
//echo '连接成功'     //可以将此行头部注释去掉,检验是否连接数据库成功。
?>


首页代码
<a href='add.php'><||>send weibo<||></a>   <a href='index.php'><||>home<||></a>
<form action="" method="get">
<input type="text" name="keys">
<input type="submit" name="subs" value="Search">
<form>
<?php
include("php-mysql.php");
if(!empty($_GET['keys'])){
//echo $k=$_GET['keys'];
$seaech="`title` like '%".$_GET['keys']."%' ";
}else{
$seaech=1;
}
//  echo $seaech;
$xianshi="select * from `weibo` where $seaech order by id desc limit 10"; //mysql语句
//            select * from `weibo` where `title` like '%s%' order by id desc limit 10      `title` like '%'s'%'
$query=mysql_query($xianshi);  //执行$xianshi 函数,将mysql 语句执行
while($rs=mysql_fetch_array($query)){
?>
<h2><a href="view.php?id=<?php echo $rs['id'];?>">title:<?php echo iconv_substr($rs['title'],0,5,"gbk")?> <!-- 使用 iconv_substr 函数 截取title 前5位。 "view.php?id=<?php echo $rs['id'];?> -->
| <a href="edit.php?id=<?php echo $rs['id']?>">   edit</a>  |<a href="del.php?del=<?php echo $rs['id']?>"> delete</a>|</h2>
<br>
<h4>time:<?php echo $rs['dates']?><br>
<h3>Content:<textarea rows="5" cols="50" name="con"><?php echo iconv_substr($rs['connects'],0,15,"gbk")?></textarea><br>
<!--<input type="submit" name="sub" value="publish">
--></form>
<?php
}
?>


添加微博页

<?php
include("php-mysql.php");
if(!@$f=fopen("mysql.txt","r")){
$mysql=0;
}
else
{
$mysql=fgets($f,10);
fclose($f);
}
$mysql++;
$ff=fopen("mysql.txt","w");
fwrite($ff,$mysql);
fclose($ff);
//$mysql=str_split($mysql);
//foreach($mysql as $v);
//上面这段代码,只起到给数据库做计算id的功能,利用的是计数器的实现机制。

//echo "连接成功" //可以将前面的注释去掉验证是否连接成功
if(!empty($_POST['sub'])){   //这里面的$_POST 一定要注意大写。判断$_POST['sub']中的值是不是空值,利用!取反,不是空值执行下面语句。
$title=$_POST['title']; //获取POST 里面的title 值赋给$title  POST注意大写
$con=$_POST['con'];     //获取POST 里面的title 值赋给$title

$sql="insert into `weibo`(`id`,`title`,`dates`,`connects`)values('$mysql','$title',now(),'$con')";
//insert into `weibo`(id,title,dates,connects)values(1,'hahhaha',now(),'sssssss')  可以仿照该sql语句写。
/*将一些sql语句赋给$aql
sql语句中的$title 获取的是$_POST['title']中的值,也就是我们在标题中输入的值,
$con 获取的是$_POST['con']中的值,也就是我们在内容中输入的值,
当中的1,是指ID值,我成绩设置了null (空)给它,但是一直没能过把数值写进数据库。后来我把1赋值给他了,
*/
mysql_query($sql);
//  echo $sql;      //用于测试显示写入数据库内容
echo "<hr>";
echo "<a href='index.php'>添加成功 回到首页</a>";

}
?>

<form action="add.php" method="post">
<hr>
标题<input type="text" name="title"><br>
内容<textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="发布">
</form>


删除页

<?php
include("php-mysql.php");
if(!empty($_GET['del'])){
$del=$_GET['del'];
$sql="delete from `weibo` where id='$del'";
mysql_query($sql);
}
echo "<a href='index.php'>Delete Complete Home!!!</a>";
?>


编辑页

<?php
include("php-mysql.php"); //连接数据库
if(!empty($_GET['id'])){   //这里面的 id 对应的是首页中单击edit 转跳 edit.php?id 这里的id  若这里写其他的,GET['随之而改']
$id=$_GET['id'];  //将get传过来的id 赋给我们这里定义的$id  。方便下面调用
$sql="select * from `weibo` where id='$id'";  //读取·weibo·数据库中id等于$id 的数据
$query=mysql_query($sql);//将执行结果赋给$query
$rs=mysql_fetch_array($query);  //用mysql_fetch_attay来读取资源,并将资源赋值给$rs
//echo $rs['title'];

}  //if结束
if(!empty($_POST['sub'])){    //判断用户有没有单击sub 提交按钮,如果按了,执行以下语句:
$title=$_POST['title'];
$con=$_POST['con'];
$hid=$_POST['hid'];  //利用下面的post中的hid 获取id值

$sql2="update `weibo` set `title`='$title',`connects`='$con' where id='$hid'";
mysql_query($sql2);
//echo "<script>alert('update');localtion.href='index.php'</script>";
echo "<a href='index.php'>Home inserted successfully </a>";
}
?>
<form action="edit.php" method="post">
<input  type='hidden' name='hid' value=<?php echo $rs['id'];//获取id值 注意,在value后面千万不能够加引号?>><!--隐藏框  目的是我可以利用 $hid=$_POST['hid'] 获取id-->
<hr>
title:<input type="text" name="title" value=<?php echo $rs['title'];//获取title的值显示在这里?> ><br>  <!-- 将$rs 资源中的'title' 给value  显示在title 文本框中-->
connect:<textarea rows="5" cols="50" name="con"><?php echo $rs['connects']?></textarea><br><!-- 将$rs 资源中的'connects'   显示在connect 文本框中-->
<input type="submit" name="sub" value="fabu">
</form>
浏览微博详细页

<?php
include("php-mysql.php");
if(!empty($_GET['id'])){
$id=$_GET['id'];   //将id赋给$id
$sql="select * from `weibo` where id='$id'";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
$update="update `weibo` set hist=hist+1 where id='$id'";   //hist=hist+1 是一个mysql函数,没执行一次语句,hist字段着增加1
mysql_query($update);
}
?>
<h1 ><a href="index.php">home</a></h1>
<h1> title: <?php echo $rs['title']?></h1>
<h4>date:<?php echo $rs['dates']?></h4>
<h3>Hits:<?php echo $rs['hist']?></h3>
<hr></hr>
<h2> connects: <?php echo $rs['connects']?></h2>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息