php - 如何在特定时间间隔重置数据库中的值?

标签 php mysql codeigniter

我正在使用PHP Codeigniter。对于系统,我希望允许经销商为其自己的用户添加平衡。条件是他只能为每个用户分配 5 欧元,不能少于也不能多于 5。假设经销商当前余额是 20 欧元,那么他应该能够仅分配 5 欧元,因此 4 个用户每人将获得 5 欧元,最重要的是,我想在每 30 天后将用户余额重置为零,我的意思是一个月末。经销商余额按照其在用户中的划分扣除,而用户余额每30天清零。我真的很困惑从哪里开始以及如何开始!

Controller 代码:

public function edit ($id = NULL)
            {

                  $usertype=$this->session->userdata('usertype');
                  if($usertype ==="admin")
                    {

                        // Fetch a user or set a new one
                        if ($id) {
                        $this->data['user'] = $this->reseller_m->get($id);
                        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
                    }
                  else 
                  {
                        $this->data['user'] = $this->reseller_m->get_new();
                  }

                        // Set up the form
                        $rules = $this->reseller_m->rules_admin;
                        $id || $rules['password']['rules'] .= '|required';
                        $this->form_validation->set_rules($rules);

                        // Process the form
                        if ($this->form_validation->run() == TRUE) {

        $data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','country_code','created','modified','status'));


                    $data['password'] = $this->reseller_m->hash($data['password']);

                    $key=$this->reseller_m->save($data, $id);

                //here we get the last inserted record id in $last_id// 
                    $last_id = $this->db->insert_id();

                //The logic to create key to uniquely identify a reseller.

                    $values=array($this->input->post('name'),$this->input->post('country_code'),$this->input->post('allocation_block'),$this->input->post('user_num'));

                    $key=implode('-',$values);

                    $this->db->where('id',$last_id);
                    $this->db->update('reseller',array('key'=>$key));

                    //Loop To Creates Users Dynamically as user_num value.

                    $this->reseller_m->get($id);


                    if($id === NULl)
                    {
                        for($i=1; $i<=$data['user_num'];$i++)
                        {

                        $userdata=array('key'=>$key);

                        $this->db->insert('users',$userdata);

                        }
                    }

                    redirect('admin/reseller');
                }

                // Load the view
                $this->data['subview'] = 'admin/reseller/edit';
                $this->load->view('admin/_layout_main', $this->data);
            }
            else
            {

                $this->load->view('permission');
            }


            }

最佳答案

请为此使用 Mysql 事件调度程序。

有关更多详细信息,请点击以下链接 - https://dev.mysql.com/doc/refman/5.1/en/events.html

关于php - 如何在特定时间间隔重置数据库中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32137613/

相关文章:

mysql - 在 MySql 中将 varchar 列值转换为整数

mysql - SQL 连接三个表并拆分为列

php - "Proper"index.php 和前端 Controller 的分离/区别

mysql - 获取每组行的最大值的正确 ID

SQL语句中的PHP变量不起作用

php - codeigniter 日期和时间在 View 中无法正确转换

php - MailChimp 列表 ID 无效

php - 我无法从表中获取名称

php - 如何从数据库中获取值以在 PHP 中进行验证?

php - 使用 SQL 和 PHP 进行多重 INSERT/UPDATE