0

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

7
  • Forgot...i already tried adding JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE to the json encode....but nothing... Commented Apr 18, 2017 at 11:13
  • What is the format of your dates? Commented Apr 18, 2017 at 11:15
  • MySQL date field, so Y-m-d Commented Apr 18, 2017 at 11:17
  • I am, of course, talking about the dates you use as inputs, I don't believe those are coming directly from MySQL. Commented Apr 18, 2017 at 11:20
  • yes, got it..and date comes in date format..in fact i see the query formed used only the dates... and the query is ok Commented Apr 18, 2017 at 11:22

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.