【问题标题】:How to update image in database using ajax in codeigniter?如何在codeigniter中使用ajax更新数据库中的图像?
【发布时间】:2019-03-03 00:59:42
【问题描述】:

我想在 CodeIgniter 中使用 ajax 更新图像。它给出了一个未知索引“employeePicture”的错误。这是为更新选择图像的表单。

<form method="post" enctype="multipart/form-data" id="updateData">
<div class="col-md-3">
    <div class="form-group">
        <label for="EditcontactNoSelector">Employee Picture</label>
        <input type="file" name="employeePicture" id="Editemployee_picture">
    </div>
</div>

这是 Ajax 代码。

  var formData = new FormData($("#updateData")[0]);
$.ajax({
    url: "'.base_url().'Employees/master_update_employees",
    type: "post",
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function(output) {
        var data = output.split("::");
        if (data[0] === "OK") {
            Shafiq.notification(data[1], data[2]);
            oTable.fnDraw();
            $("#employeePicture").val("");


        } else if (data[0] === "FAIL") {
            Shafiq.notification(data[1], data[2]);
        }
    }
});

这是将数据更新到数据库的函数。现在在这里它给出了一个关于“employeePicture”的错误。

<?php
public

function master_update_employees()
    {
    if ($this->input->post())
        { //If Any Values Posted
        if ($this->input->is_ajax_request())
            { //If Request Generated From Ajax

            // Getting Posted Values

            $employee_picture = $_FILES['employeePicture']['name'];
            $path = 'assets/employee_profile/' . $employee_picture;
            move_uploaded_file($_FILES["employeePicture"]["tmp_name"], $path);
            $Name = $this->input->post('Name');
            $Contact = $this->input->post('Contact');
            $Mobile = $this->input->post('EditMobile');
            $EditCNIC = $this->input->post('EditCNIC');
            $FatherName = $this->input->post('FatherName');
            $ID = $this->input->post('ID');
            $Address = $this->input->post('Address');
            $designation = $this->input->post('EditDesignation');
            $shift = $this->input->post('EditShift');
            $joinDate = $this->input->post('EditJoiningDate');
            $basicSalary = $this->input->post('EditBasicSalary');
            $PermanentAddress = $this->input->post('EditPermanentAddress');
            $IsEnabled = $this->input->post('Enabled');
            $Enabled = 1;
            if ($IsEnabled == "true")
                {
                $Enabled = 1;
                }
            elseif ($IsEnabled == "false")
                {
                $Enabled = 0;
                }

            $table = "employees";
            $updateData = array(
                'Name' => $Name,
                'Father_Name' => $FatherName,
                'Phone' => $Contact,
                'Mobile' => $Mobile,
                'Designation' => $designation,
                'shift' => $shift,
                'JoinDate' => $joinDate,
                'BasicSalary' => $basicSalary,
                'CNIC' => $EditCNIC,
                'Pres_Address' => $Address,
                'Picture' => $path,
                'Perm_Address' => $PermanentAddress,
                'IsEnabled' => $Enabled
            );
            $updateWhere = array(
                'id' => $ID
            );
            $result = $this->Common_model->update($table, $updateWhere, $updateData);
            if ($result === true)
                {
                echo "OK::Record Successfully Updated::success";
                return;
                }
              else
                {
                if ($result['code'] === 0)
                    {
                    echo "FAIL::Record is Same in Database, No Change Occurred::warning";
                    }
                  else
                    {
                    echo "FAIL::";
                    print_r($result);
                    echo "::error";
                    }

                return;
                }
            }
        }
    } // update Employee

【问题讨论】:

  • 在更新函数的第一行添加print_r($_FILES)并在开发工具网络窗格中查看响应。它是什么?只是array()?
  • 你应该检查这个Answer
  • 它给了我.Array ( [employeePicture] => Array ( [name] => aneesa.jpg [type] => image/jpeg [tmp_name] => D:\Xampp\tmp\ phpC775.tmp [错误] => 0 [大小] => 18911 ) ) @Alex
  • 那么索引 employeePicture 不应该是未定义的,例如给你那个错误。您得到该错误的唯一方法是如果您上传图片,因为您没有事先检查它是否存在。
  • 那么为什么它在网络窗格中给我一个错误。那“未定义的索引:员工图片”??

标签: php ajax codeigniter


【解决方案1】:

由于您的员工照片很好,我假设您的问题是您没有上传照片,因为您没有检查它是否存在。以这种方式重新排列了您的代码。

$updateData = array(
                    'Name' => $Name,
                    'Father_Name' => $FatherName,
                    'Phone' => $Contact,
                    'Mobile' => $Mobile,
                    'Designation'=>$designation,
                    'shift'=>$shift,
                    'JoinDate'=>$joinDate,
                    'BasicSalary'=>$basicSalary,
                    'CNIC' => $EditCNIC,
                    'Pres_Address' => $Address,
                    //'Picture' => $path,
                    'Perm_Address' => $PermanentAddress,
                    'IsEnabled' => $Enabled
                );

if (isset($_FILES['employeePicture'])) {
    $employee_picture=$_FILES['employeePicture']['name'];
    $path='assets/employee_profile/'.$employee_picture;
    move_uploaded_file($_FILES["employeePicture"]["tmp_name"], $path);
    $updateData['Picture'] = $path;
}

【讨论】:

  • 此代码不起作用
  • 不工作是没有建设性的。 究竟是什么不起作用?
猜你喜欢
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2018-11-24
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多