数据库 weibo
DROP TABLE IF EXISTS `msgs`;
CREATE TABLE `msgs` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) unsigned DEFAULT NULL,
`msg` text,
`createtime` int(10) unsigned DEFAULT NULL,
`loves` int(11) unsigned DEFAULT '0',
`hates` int(11) unsigned DEFAULT '0',
`isdel` bit(1) DEFAULT b'0',
PRIMARY KEY (`Id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `msgs` VALUES (10,1,'测试、。',1534815070,0,0,b'0'),(32,1,'用户:ken 等级:5',1534839205,0,0,b'0'),(33,1,'用户:ken 等级:52ml',1534839236,0,0,b'0');
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
`pwd` varchar(32) DEFAULT NULL,
`age` tinyint(3) unsigned DEFAULT NULL,
`level` tinyint(3) unsigned DEFAULT NULL,
`birthday` int(10) unsigned DEFAULT '1500000000',
`flows` int(11) unsigned DEFAULT '0',
`avartar` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `users` VALUES (1,'ken','111',25,5,1500000000,1010,NULL),(2,'john','222',21,1,1500000000,0,NULL);
后端 api.php
1,"state"=>"success","user"=>$_SESSION['user']));
}
else{
$stmt = $mysqli->prepare("SELECT * FROM users WHERE `name`= ? and pwd = ?");
$stmt->bind_param('ss', $username,$pwd);
$username=$_REQUEST['username'];
$pwd=$_REQUEST['pwd'];
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
if($row){
$_SESSION['user']=$row;
echo json_encode(array("code"=>1,"state"=>"success","user"=>$row));
}
else{
echo json_encode(array("code"=>0,"state"=>"fail","username"=>$username,"pwd"=>$pwd));
}
mysqli_free_result($res);//释放资源
}
break;
case "addmsg"://发送信息
if(isset($_SESSION['user']))
{
$u=$_SESSION['user'];
$a=$u['Id'];
if(isset($_REQUEST['msg']))
{
$b=$_REQUEST['msg'];
$c=time();
$stmt = $mysqli->prepare("insert into msgs(userid,msg,createtime) value(?,?,?)");
$stmt->bind_param('isi',$a,$b,$c);
$stmt->execute();
$msgid=mysqli_insert_id($mysqli);
echo json_encode(array("code"=>1,"state"=>"success","msgid"=>$msgid,"createtime"=>$c));
}
else{
echo json_encode(array("code"=>0,"state"=>"fail"));
}
}
else{
echo json_encode(array("code"=>0,"state"=>"fail","msg"=>"you have no right to add msg!"));
}
break;
case "chklogin": //检查用户登录状态 code: 1已登录 , 0未登录
if(isset($_SESSION['user']))
{
echo json_encode(array("code"=>1,"msg"=>"success","user"=>$_SESSION['user']));
}
else{
echo json_encode(array("code"=>0,"msg"=>"not logined"));
}
break;
case "getmsgs"://读取信息 p 当前页码
$pagesize=10;
$page=1;
if(isset($_REQUEST['p']))
$page=intval($_REQUEST['p']);
$offset=($page-1)*$pagesize;
$sql="SELECT * FROM msgs ORDER BY id desc limit ".$offset.",".$pagesize;
$result=mysqli_query($mysqli,$sql);
// 获取数据
$rows=mysqli_fetch_all($result,MYSQLI_ASSOC);
$result2=mysqli_query($mysqli,"select count(*) as cc from msgs ");
$totalrecord=mysqli_fetch_assoc($result2);
if($rows)
echo json_encode(array("code"=>1,"state"=>"success","msgs"=>$rows,"totalrecord"=>intval($totalrecord["cc"])));
else
echo json_encode(array("code"=>0,"state"=>"fail"));
// 释放结果集
mysqli_free_result($result);
break;
case "op": //常用操作 op :1赞,2踩 ,3删除信息 ; msgid:信息编号 ; 返回 json : code (0失败 ,1成功) ,msg 操作结果说明
$op=0;
$msgid=0;
if(isset($_REQUEST['op']))
$op=intval($_REQUEST['op']);
if(isset($_REQUEST['msgid']))
$msgid=intval($_REQUEST['msgid']);
if($op>0&&$msgid>0)
{
$sql='';
switch($op)
{
case 1:$sql="update msgs set loves=loves+1 where id=".$msgid;break;
case 2:$sql="update msgs set hates=hates+1 where id=".$msgid;break;
case 3:
if(isset($_SESSION['user']))
{
$u=$_SESSION['user'];
$sql="delete from msgs where id=".$msgid." and userid=".$u['Id'];
}
else{
echo json_encode(array("code"=>0,"state"=>"fail","msg"=>"you have no right to del msg!"));
}
break;
}
if(!empty($sql))
{
$result=mysqli_query($mysqli,$sql);
if($result)
echo json_encode(array("code"=>1,"state"=>"success","msg"=>"op success!"));
else
echo json_encode(array("code"=>1,"state"=>"success","msg"=>"nothing changed!"));
}
else{
echo json_encode(array("code"=>0,"state"=>"fail","msg"=>"unknow op parameter"));
}
}
else{
echo json_encode(array("code"=>0,"state"=>"fail","msg"=>"unknow op parameter"));
}
break;
default:
echo '{"msg":"no such action found!"}';
}
}
else{
echo '{"msg":"no action argument!"}';
}
?>
前台 weibo.html
加载中。。。