The detailed process of PHP operation Redis addition, deletion, modification and checking

First open the Redis service and connect to the local Redis service. Xiaobian connects like this. There are many methods.

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


Create a form

<!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>

adding data

<?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 "添加失败";
    }
 ?>

Display data plus pagination

<?php
    header("content-type:text/html;charset=utf-8");
require("redis.php");
//Get paging -- first know the total number, the number of items per page, the current number of pages, Total number of pages
//Total number of users
$count = $redis->lsize("uid");
//Number of entries per page
$page_size = 3;
//Current number of pages
$page_num = (!empty($_GET['page'] ))?$_GET['page']:1;
//Total number of pages
$page_count = ceil($count/$page_size);
$ids = $redis->lrange("uid",($page_num-1)*$ page_size,(($page_num-1)*$page_size+$page_size-1));
//Get all current users
/*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);//Filter empty elements in the array
?>
<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>

Delete Files

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

Modify the file

<?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>

perform modification

<?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 "修改失败";
}
?>




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847815&siteId=291194637