首页 > 解决方案 > 防止在数据库 Codeigniter 中重复输入

问题描述

我想知道如何防止我的上传功能使用 uid 将重复条目插入数据库。

public function upload(){
        if(!empty($_FILES['uploaded_file']))
        {
            $path = FCPATH . "/file_attachments/signature_file/";
            $path = $path . basename( $_FILES['uploaded_file']['name']);

            $base64 = base64_encode(file_get_contents($_FILES['uploaded_file']['tmp_name']));

            if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) 
            {
                $data = array (
                        'uid' => $this->user->get_uid(),
                                    'image' => $base64,
                                    'name'  => basename( $_FILES['uploaded_file']['name']),
                                );


                $this->load->database('employee');         
                $this->db->insert('signatures', $data);

    //              echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
    //              " has been uploaded";
                  $alert  = "The file ".  basename( $_FILES['uploaded_file']['name']). 
                  " has been uploaded";
                     redirect(base_url() . "signature_uploader/search/?id=" . $alert);                
            } 
            else
            {
                // echo "There was an error uploading the file, please try again!";
                $alert  ="There was an error uploading the file, please try again!";
                redirect(base_url() . "signature_uploader/search/?id=" . $alert); 
            }       
        }        
}

这是我的视图文件。我没有时间在这里编辑不必要的东西。问题仍然是,如果它有重复的条目,数据库错误仍然会出现,这就是我阻止这样做的原因。干杯!

<div class="bod">
<div class="insta">
    <form enctype="multipart/form-data" style="padding-top: 10px" name="exit_form" method="post" action="<?= base_url() ?>signature_uploader/upload">
    <input type="hidden" name="uid" value="<?= $agent->get_uid() ?>" />
    <input type="hidden" name="gid" value="<?= $agent->get_gid() ?>" />
    <input type="hidden" name="fname" value="<?= $agent->get_fname() ?>" />
    <input type="hidden" name="lname" value="<?= $agent->get_lname() ?>" />
    <div style="text-align: center; font-size: 25pt; margin-bottom: 15px">Signature Uploader</div>
    <table width="105%">
        <tr>
            <td width="40%"><strong>Name: </strong><input class="textinput" disabled="disabled" type="text" name="full_name" id="textfield3" size="35" value="<?= $agent->get_fullName() ?>" /></td>
        <tr/>
        <tr>
            <td><label>Upload Image File:</label><br /> <input name="uploaded_file" type="file" accept=".png" required/>
        </tr>
    <table/>
    <br />
    <input type="submit" value="Submit" class="button1" />
    </form>
    <br/>
&nbsp;
</div>

 

标签: codeignitercodeigniter-3

解决方案


您可以使用 Codeigniter 表单验证来防止重复输入:

首先,您必须uid像这样在表单视图中获得:

<input type="hidden" name="uid" value="<?php echo $this->user->get_uid() ?>">

然后在你的控制器中:

public function upload(){
    if(!empty($_FILES['uploaded_file']))
      {
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->form_validation->set_rules('uid', 'UID', 'is_unique[signatures.uid]');
            if ($this->form_validation->run() == FALSE)
            {
                // Your Code if the uid is duplicate
                $alert  ="Could't upload because of duplicate entry!";
                redirect(base_url() . "signature_uploader/search/?id=" . $alert); 
            }
            else
            {
                    // Your Code if the uid is unique
                $path = FCPATH . "/file_attachments/signature_file/";
                $path = $path . basename( $_FILES['uploaded_file']['name']);
                $base64 = base64_encode(file_get_contents($_FILES['uploaded_file']['tmp_name']));
                if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
                    $data = array (
                        'uid' => $this->input->post('uid'),
                        'image' => $base64,
                        'name'  => basename( $_FILES['uploaded_file']['name']),
                    );
                    $this->load->database('employee');         
                    $this->db->insert('signatures', $data);

    //              echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
    //              " has been uploaded";
                  $alert  = "The file ".  basename( $_FILES['uploaded_file']['name']). 
                  " has been uploaded";
                     redirect(base_url() . "signature_uploader/search/?id=" . $alert);                
                } else{
    //                echo "There was an error uploading the file, please try again!";
                    $alert  ="There was an error uploading the file, please try again!";
                    redirect(base_url() . "signature_uploader/search/?id=" . $alert); 
                }
            }
      }        
}

推荐阅读