如何在Codeigniter中创建用于更新数据库的增量按钮


How to create incrementing button in Codeigniter that updates database onclick?

我是新手。我有一个项目,其中管理分配平衡的经销商。现在我想在html中创建一个按钮,它为管理员提供接口,以增加按钮单击上的余额。当他增加它的值也应该在数据库中更新。也许这很简单,但我不能这样做!我觉得应该同时使用ajax或Jquery

下面是我的代码视图:(想要增加增量和减量按钮除了平衡输入)

<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
    <tr>
        <td>SIP Username</td>
        <td><?php echo form_input('sip_username', set_value('sip_username', $user->sip_username)); ?></td>
    </tr>
    <tr>
        <td>SIP Password</td>
        <td><?php echo form_input('sip_password', set_value('sip_password', $user->sip_password)); ?></td>
    </tr>
    <tr>
        <td>Key</td>
        <td><?php echo form_input('key', set_value('key', $user->key), 'readonly'); ?></td>
    </tr>
    <tr>
        <td>Allocation Block</td>
        <td><?php echo form_input('allocation_block', set_value('allocation_block', $user->allocation_block)); ?></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
    </tr>   
    <tr>
        <td>Reseller Email</td>
        <td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
    </tr>   
    <tr>
        <td>Password</td>
        <td><?php echo form_password('password'); ?></td>
    </tr>   
    <tr>
        <td>Confirm password</td>
        <td><?php echo form_password('password_confirm'); ?></td>
    </tr>
    <tr>
        <td>User_Required</td>
        <td><?php echo form_input('user_num', set_value('user_num', $user->user_num)); ?></td>
    </tr>
    <tr>
        <td>Balance</td>
        <td><?php echo form_input('balance', set_value('balance', $user->balance)); ?></td>
    </tr>
    <tr>
        <td>Phone</td>
        <td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
    </tr>
    <tr>
        <td>Address</td>
        <td><?php echo form_input('address', set_value('address', $user->address)); ?></td>
    </tr>
    <tr>
        <td>Status</td>
        <td><?php echo form_dropdown('status', array('Active' => 'Active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>  
    </tr>
    <tr>
        <td>Country</td>
        <td><?php echo form_input('country', set_value('country', $user->country)); ?></td>
    </tr>
    <tr>
        <td>Country Code</td>
        <td><?php echo form_input('country_code', set_value('country_code', $user->country_code)); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
    </tr>
    </table>
    <?php echo form_close();?>

The Controller:

public function edit ($id = NULL)
{
    // 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 blank rows in user table mapped to reseller_id    
        $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));
            for($i=1; $i<=$data['user_num'];$i++)
            {
            $userdata=array('key'=>$key);
        // here users is taken name of user table with retailer_id is field
             $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);
}

如果您熟悉ajax,您可以使用ajax。

单击按钮,您可以调用ajax并在控制器函数中编写代码来更新数据库中的值。