繁体   English   中英

回声 json_encode($data); 控制器中的 PHP CodeIgniter 错误

[英]echo json_encode($data); Error in PHP CodeIgniter in Controller

Hai 我正在使用 CodeIgniter 3 中的管理面板做一个 crud 应用程序。我想更新表中的图像。 我在 echo json_encode 中收到未定义的数据错误,请帮我解决这个问题。 在这里,我将展示我的代码,请检查。

脚本

$(document).on("click", "#update", function(e) {
    e.preventDefault();

    var edit_id = $("#edit_record_id").val();
    var edit_empid = $("#edit_empid").val();
    var edit_name = $("#edit_name").val();
    var edit_email = $("#edit_email").val();
    var edit_phone = $("#edit_phone").val();
    var edit_location = $("#edit_location").val();
    var edit_salary = $("#edit_salary").val();
    var edit_bonus = $("#edit_bonus").val();
    var edit_img = $("#edit_img")[0].files[0];

    if (edit_id == "" || edit_empid == "" || edit_name == "" || edit_email == "" || edit_phone == "" || edit_location == "" || edit_salary =="" || edit_bonus == ""|| edit_img == "") {
        alert("All field are required");
    } else {
        var fd = new FormData();

        fd.append("edit_id", edit_id);
        fd.append("edit_empid", edit_empid);
        fd.append("edit_name", edit_name);
        fd.append("edit_email",edit_email);
        fd.append("edit_phone", edit_phone);
        fd.append("edit_location", edit_location);
        fd.append("edit_salary", edit_salary);
        fd.append("edit_bonus", edit_bonus);
        if ($("#edit_img")[0].files.length > 0) {
            fd.append("edit_img", edit_img);
        }

        $.ajax({
            type: "post",
            url: base_url + "update",
            data: fd,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function(response) {
                if (response.res == "success") {
                    toastr["success"](response.message);
                    $("#edit_modal").modal("hide");
                    $("#edit_form")[0].reset();
                    $(".edit-file-label").html("Choose file");
                    $("#records").DataTable().destroy();
                    fetch();
                } else {
                    toastr["error"](response.message);
                }
            },
        });
    }
});

控制器:User.php

    public function update()
    {
        if($this->input->is_ajax_request()) {
            $this->form_validation->set_rules('edit_empid', 'EmployeID','required');
            $this->form_validation->set_rules('edit_name', 'Name','required');
            $this->form_validation->set_rules('edit_email', 'Email','required|valid_email');
            $this->form_validation->set_rules('edit_phone', 'Phone','required');
            $this->form_validation->set_rules('edit_location', 'Location','required');
            $this->form_validation->set_rules('edit_salary', 'Salary','required');
            $this->form_validation->set_rules('edit_bonus', 'Bonus','required');
            if ($this->form_validation->run() == false) {
                $data = array('res'=>'error', 'message' => validation_errors());
            }else{
                if (isset($_FILES["edit_img"]["name"])) {
                    $config['upload_path'] = APPPATH . '../assets/uploads/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size']     = '1000';
                    // $config['max_width'] = '1024';
                    // $config['max_height'] = '768';
                    $this->load->library('upload', $config);

                    if (!$this->upload->do_upload("edit_img")) {
                        $data = array('res' => "error", 'message' => $this->upload->display_errors());
                    } else {
                            $id= $this->input->post('edit_id');
                            $data1= array(
                                'empid'=>$this->input->post('edit_empid'),
                                'name'=>$this->input->post('edit_name'),
                                'email'=>$this->input->post('edit_email'),
                                'phone'=>$this->input->post('edit_phone'),
                                'location'=>$this->input->post('edit_location')
                            );

                            $data2=array(
                                'empid'=>$this->input->post('edit_empid'),
                                'salary'=>$this->input->post('edit_salary'),
                                'bonus'=>$this->input->post('edit_bonus'),
                                'img'=>$this->input->post()
                            );
                            $data2['img'] = $this->upload->data('file_name');
                                if ($this->users_model->update_entry($id, $data1,$data2)) {
                                    $data = array('res' => 'success', 'message' => 'Record update Successfully');
                                }
                    }
                }
            }
            echo json_encode($data);
        }else{
            echo "No direct Script access allowed";
        }
    }

模型

        public function update_entry($id, $data1,$data2)
        {
            $this->db->where("empid",$id);
            $this->db->update("crud",$data1);
            $this->db->where("empid",$id);
            $this->db->update("salary",$data2);
             return true;
        }

错误:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: data

Filename: controllers/User.php

Line Number: 237

Backtrace:

File: C:\xampp\htdocs\codeigniter_login_ajax4\application\controllers\User.php
Line: 237
Function: _error_handler

File: C:\xampp\htdocs\codeigniter_login_ajax4\index.php
Line: 315
Function: require_once

请帮我找出问题所在。 谢谢

在检查前声明$data

$data = [];
if ($this->form_validation->run() == false) {
...
} else {
...
}
echo json_encode($data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM