dtqysxw4659 2017-04-18 11:11
浏览 32

PHP,jQuery和JSON,日期搜索

I have this situation:

All the search i do via the form works perfectly, but when i set dates it return nothing...also if query is valid.. any idea ?

JS PAGE

$( '#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 PAGE

$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));
    }

So, basically my problem is that whatever param i use for the search it works...

BUT if i set 1 or both dates it give me response 200 and no data....

any ideas ?

-- UPDATE --

Query formed used dates

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

weird

It return me error with status 200....thats even more weird

  • 写回答

1条回答 默认 最新

  • douhe1002 2017-04-18 12:42
    关注

    SOLVED

    just runned

    json_last_error()
    

    And it was an utf-8 encoding error

    so... just used the solution proposed by Tiago here

    Thanks everybody for the time you dedicated to me.

    Have a nice day

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序