php CodeIgniter 3 -数据库中的函数搜索值返回错误500

lh80um4z  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(149)

我尝试搜索值(ean),如果存在于数据库返回标题产品分配给这个ean。
我有两张table:

products

product_details

在产品表中,我有EAN,在product_details中,我有title。

products.id  = product_details.product_id

查看:

<div class="form-group">

            <label><?= trans("search"); ?></label>

            <input type="text" id="input_product_exist" class="form-control" placeholder="<?= trans("category_name"); ?>">

            <div id="product_search_result" class="category-search-result"></div>

        </div>

视图中的脚本:

<script>
$(document).on("input", "#input_product_exist", function () {

    var val = $(this).val();

    val = val.trim();

    if (val.length > 1) {

        var data = {

            "ean": val,

            "sys_lang_id": sys_lang_id

        };

        data[csfr_token_name] = $.cookie(csfr_cookie_name);

        $.ajax({

            type: "POST",

            url: base_url + "ajax_controller/search_products",

            data: data,

            success: function (response) {

                var obj = JSON.parse(response);

                if (obj.result == 1) {

                    document.getElementById("product_search_result").innerHTML = obj.content;

                }

            }

        });

    } else {

        document.getElementById("product_search_result").innerHTML = "";

    }

});

ajax控制器:

//search products
public function search_products()
{
    $ean = $this->input->post('ean', true);
    $products = $this->category_model->search_products_by_ean($product_ean);
    $content = '<ul>';
    if (!empty($products)) {
        foreach ($products as $item) {
            $content .= '<li>' . html_escape($item->name) . ' - <strong>' . trans("id") . ': ' . $item->id . '</strong></li>';
        }
        $content .= '</ul>';
    } else {
        $content = '<p class="m-t-15 text-center text-muted">' . trans("no_records_found") . '</p>';
    }
    $data = array(
        'result' => 1,
        'content' => $content
    );
    echo json_encode($data);
}

category_model.php

//search products by ean
public function search_products_by_ean($product_ean)
{
    $this->db->select('products.id, product_details.title as name');
    $this->db->join('product_details', 'product_details.product_id = products.id');
    $this->db->like('name', clean_str($product_ean));
    //$this->db->where('visibility', 1);
    //$this->db->order_by('categories.parent_id');
    $this->db->order_by('name');
    $query = $this->db->get('products');
    return $query->result();
}

现在我尝试开始搜索:白页。
我签入日志控制台:

jquery.min.js:4          POST https://thisismywebsite.com/ajax_controller/search_products 500
send @ jquery.min.js:4
ajax @ jquery.min.js:4
(anonymous) @ add-product:1581
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3

@更新:
我认为我在第3步category_model/search_products_by_ean中仍有问题
这里我在phpmyadmin中构建工作查询:

SELECT products.ean, product_details.title  FROM products INNER JOIN product_details ON products.id = product_details.product_id;

我得到了正确的输出

s8vozzvw

s8vozzvw1#

@更新
你能使php错误与

error_reporting(E_ALL);
ini_set('display_errors', 1);

查看当前错误不是500
AJAX 控制器中,您必须发送$ean而不是$product_ean

//search products
public function search_products()
{
    $ean = $this->input->post('ean', true);
  //  $products = $this->category_model->search_products_by_ean($product_ean);
    $products = $this->category_model->search_products_by_ean($ean);//istead $product_ean
    $content = '<ul>';
    if (!empty($products)) {
        foreach ($products as $item) {
            $content .= '<li>' . html_escape($item->name) . ' - <strong>' . trans("id") . ': ' . $item->id . '</strong></li>';
        }
        $content .= '</ul>';
    } else {
        $content = '<p class="m-t-15 text-center text-muted">' . trans("no_records_found") . '</p>';
    }
    $data = array(
        'result' => 1,
        'content' => $content
    );
    echo json_encode($data);
}

相关问题