防止在数据库Codeigniter中重复输入

时间:2018-12-06 07:13:04

标签: codeigniter codeigniter-3

我想知道如何防止我的上载功能使用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>

2 个答案:

答案 0 :(得分:4)

您可以使用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); 
                }
            }
      }        
}

答案 1 :(得分:0)

您可以检查天气是否存在这样的文件,但是通常在创建上传文件的代码时,该函数具有为我们上传的每个文件生成唯一名称的功能。因此,在这种情况下,它将具有名称不同的重复条目。

我看到的唯一方法是创建一个函数,以检查天气文件名已存在于数据库中。