关于redis增删查改的步骤

来源:互联网 发布:枪神纪刷枪软件 免费 编辑:程序博客网 时间:2024/05/16 14:40

首先开启Redis服务,连接本地的Redis服务,小编是这样连接,方法有多种。

<?php
$redis = new Redis();
$link = $redis->connect('127.0.0.1', 6379);
?>


创建表单

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<center>
    <form action="tianjia.php" method="post">
    <table>
        <tr>
            <td>商品名称</td>
            <td><input type="text" name="t_title"></td>
        </tr>
        <tr>
            <td>商品数量</td>
            <td><input type="text" name="t_age"></td>
        </tr>
        <tr>
            <td>商品详情</td>
            <td><textarea name="t_content" id="" cols="30" rows="10"></textarea></td>
        </tr>
        <tr>
            <td><input type="submit" value="添加"></td>
            <td></td>
        </tr>
    </table>
  </form>
</center>
</body>

</html>

添加数据

<?php
    header("content-type:text/html;charset=utf-8");
    require("redis.php");
    $t_title = $_POST['t_title'];
    $t_age = $_POST['t_age'];
    $t_content = $_POST['t_content'];
    $uid = $redis->incr("userid");
    $res = $redis->hmset("user:".$uid,array("uid"=>$uid,"t_title"=>$t_title,"t_age"=>$t_age,"t_content"=>$t_content));
    //var_dump($res);die;
    $redis->rpush("uid",$uid);
    //var_dump($arr);die;
    if($res){
        header("location:zhanshi.php");
    }else{
        echo "添加失败";
    }
 ?>

展示数据加分页

<?php
    header("content-type:text/html;charset=utf-8");
require("redis.php");
//获取分页--首先知道总数,每页条数,当前页数,页总数
//用户总数
$count = $redis->lsize("uid");
//每页条数
$page_size = 3;
//当前页数
$page_num = (!empty($_GET['page']))?$_GET['page']:1;
//页总数
$page_count = ceil($count/$page_size);
$ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1));
//取出当前的所有用户
/*for($i=1;$i<=($redis->get("userid"));$i++){
$data[] = $redis->hgetall("user:".$i);
}*/
foreach($ids as $val){
$data[] = $redis->hgetall("user:".$val);
}
$data = array_filter($data);//过滤数组中的空元素
?>
<table border=1>
<tr>
               <td>uid</td>
               <td>t_title</td>
               <td>t_age</td>
               <td>t_content</td>
            <th>操作</th>
</tr>
<?php foreach($data as $val){ ?>
<tr>
            <td><?php echo $val['uid'];?></td>
                <td><?php echo $val['t_title'];?></td>
                <td><?php echo $val['t_age'];?></td>
                <td><?php echo $val['t_content'];?></td>
<td><a href="del.php?id=<?php echo $val['uid']?>">删除</a>
<a href="upload.php?id=<?php echo $val['uid']?>">编辑</a></td>
</tr>
<?php } ?>
<tr>
<td colspan="4">
<a href="?page=<?php echo (($page_num-1)<=1)?1:($page_num-1); ?>">上一页</a>
<a href="?page=<?php echo (($page_num+1)>=$page_count)?$page_count:($page_num+1); ?>">下一页</a>
<a href="?page=1">首页</a>
<a href="?page=<?php echo $page_count; ?>">尾页</a>
当前<?php echo $page_num; ?>页
总共<?php echo $page_count; ?>页
总共<?php echo $count; ?>用户
</td>
</tr>
</table>

删除文件

<?php
require("redis.php");
$uid = $_GET['id'];
$res = $redis->del("user:".$uid);
$redis->lrem("uid",$uid);
if($res){
header("location:zhanshi.php");
}
?>

修改文件

<?php
  header("content-type:text/html;charset=utf-8");

require("redis.php");
$uid = $_GET['id'];
$data = $redis->hgetall("user:".$uid);
?>
<form action="xiugai.php" method="post">
<input type="hidden" value="<?php echo $data['uid']; ?>" name="uid" />
    <table>
        <tr>
            <td>商品名称</td>
            <td><input type="text" name="t_title"></td>
        </tr>
        <tr>
            <td>商品数量</td>
            <td><input type="text" name="t_age"></td>
        </tr>
        <tr>
            <td>商品详情</td>
            <td><textarea name="t_content" id="" cols="30" rows="10"></textarea></td>
        </tr>
        <tr>
            <td><input type="submit" value="修改" /></td>
            <td></td>
        </tr>
    </table>
  </form>

执行修改

<?php
require("redis.php");
$uid = $_POST['uid'];
$t_title = $_POST['t_title'];
$t_age= $_POST['t_age'];
$t_content= $_POST['t_content'];
$res = $redis->hmset("user:".$uid,array("t_title"=>$t_title,"t_age"=>$t_age,"t_content"=>$t_content));
if($res){
header("location:zhanshi.php");
}else{
echo "修改失败";
}
?>
原创粉丝点击