【问题标题】:PHP, jQuery and JSON, date searchPHP、jQuery 和 JSON、日期搜索
【发布时间】:2017-09-14 04:43:58
【问题描述】:

我有这种情况:

我通过表单进行的所有搜索都完美无缺,但是当我设置日期时,它什么也不返回……如果查询有效……有什么想法吗?

JS页面

$( '#frm_ricerca' )
    .submit( function( e ) {
        console.log('fired');
        $("#ricerca_btn").html('Caricamento in corso...');
        table.clear().draw();
        ZEUS.Notification('bottom', 'center', 'Ricerca Avviata', 1);
        e.preventDefault();
        $.ajax( {
            url: 'index.php?dispatch&token=XXXX&method=search_by',
            type: 'POST',
            data: new FormData( this ),
            processData: false,
            contentType: false,
            success : function(datas){
              $.each (datas, function(i, v){
                 table.row.add([v.fieldA, v.fieldB...]);
              });
                $("#count_research").text(datas.length + ' Elementi trovati');
                $("#search_result").show();
                table.draw();
                $("#ricerca_btn").html('Ricerca');

            },
            error : function(e){
                console.log("---- errore ----" );
                console.log(e);
                console.log("--- FINE ERRORE ---");
                $("#ricerca_btn").html('Ricerca');
            }
        } );
    } );

PHP 页面

$searchKeys = array(
        'id_operazione'       => filter_input(INPUT_POST, 'reg_id', FILTER_SANITIZE_STRING),
        'rapporto_numero'     => filter_input(INPUT_POST, 'rapporto_numero', FILTER_SANITIZE_STRING),
        'CONCAT(Cognome, \' \', Nome) '  => filter_input(INPUT_POST, 'nominativo', FILTER_SANITIZE_STRING),
        'CodiceFiscale'  => filter_input(INPUT_POST, 'cf', FILTER_SANITIZE_STRING),
        'data_formazione >= '  => filter_input(INPUT_POST, 'data_dal', FILTER_SANITIZE_STRING),
        'data_formazione <= '  => filter_input(INPUT_POST, 'data_al', FILTER_SANITIZE_STRING),
    );


    $mainQuery = 'SELECT id_operazione, Cognome, Nome, CodiceFiscale, data_formazione FROM database WHERE ';

    foreach ($searchKeys as $field=>$value) {
        if(!empty($value)){
            if (strpos($field, 'Cognome') !== false) {
                $mainQuery .= $field . " LIKE '%$value%' AND ";
            } else if(strpos($field, 'data') !== false) {
                $mainQuery .= $field . " '$value' AND ";
            } else {
                $mainQuery .= $field . " = '$value' AND ";
            }
        }
    }

    $mainQuery = substr($mainQuery, 0, -4);

    $result = $mysqli->query($mainQuery);
    $data = array();
    if($result) {
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }

        if(empty($data)){
            http_response_code(200);
            echo json_encode(array("status"=>"succes", "message"=>"no data"));
        } else {
            http_response_code(200);
            echo json_encode($data);

        }
    } else{
        http_response_code(400);
        echo json_encode(array("status"=>"error", "query"=>$mainQuery));
    }

所以,基本上我的问题是无论我使用什么参数来搜索它都可以工作......

但如果我设置 1 或两个日期,它会给我响应 200 并且没有数据....

有什么想法吗?

-- 更新--

查询形成的使用日期

SELECT id_operazione, Cognome, Nome, CodiceFiscale, data_formazione FROM aui WHERE data_formazione >= '2017-01-01'

它返回给我状态为 200 的错误......那更奇怪

【问题讨论】:

  • 忘记了...我已经尝试添加 JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE 到 json 编码......但什么都没有......
  • 你的日期格式是什么?
  • MySQL 日期字段,所以 Y-m-d
  • 我当然是在谈论您用作输入的日期,我不相信那些直接来自 MySQL。
  • 是的,明白了..日期以日期格式出现..实际上我看到形成的查询只使用了日期...查询没问题

标签: javascript php jquery json


【解决方案1】:

已解决

刚刚运行

json_last_error()

这是一个 utf-8 编码错误

所以...刚刚使用了Tiago here提出的解决方案

感谢大家为我付出的时间。

祝你有美好的一天

【讨论】:

    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多