Home » Php » php – Flutter HTTP POST method with SQL query

php – Flutter HTTP POST method with SQL query

Posted by: admin February 25, 2020 Leave a comment

Questions:

i have this part of the code mounted in a server, what a want to do is a query with range dates, the connection with the Data base is correct.

<?php
include 'db.php';

$dateBigin = $_POST['dateBigin'];
dateBigin='".$dateBigin."';
dateEnd='".$dateEnd."'
$dateEnd = $_POST['dateEnd'];

$queryResult=$connect->query("SELECT * FROM cao_fatura INNER JOIN cao_os ON cao_fatura.co_os = cao_os.co_os WHERE cao_fatura.data_emissao >= '".$dateBigin."' AND cao_fatura.data_emissao <= '".$dateEnd."' ");

$result=array();

while($fetchData=$queryResult->fetch_assoc()){
    $result[]=$fetchData;
}

echo json_encode($result);

?>

this part of the code is en charge of the post method and is activated by a ontap() that is located in a button

  void postValue() {
    print(dateStart);
    print(dateFinish);
    String url = "http://dbname.000webhostapp.com/relatorio.php";
    http.post(url, body: {
      "dateBigin": dateStart.toString(),
      "dateEnd": dateFinish.toString()
    });
  }

print output

I/flutter (18726): 2003-12-31 00:00:00.000
I/flutter (18726): 2007-12-31 00:00:00.000

so when i pass the screen this query is executed but…

  Future<List>getValue() async {
    final response =
        await http.get("http://dbname.000webhostapp.com/relatorio.php");
          json.decode(response.body);
    return json.decode(response.body);
  }

instead of giving me the result with the data i have this terminal output

I/flutter (18726): FormatException: Unexpected character (at character 1)
I/flutter (18726): <br />
I/flutter (18726): ^

what i discover is that the problem is related with the .php format but I’m not 100% sure because when i change the .php query to this

<?php
include 'db.php';

$queryResult=$connect->query("SELECT * FROM cao_fatura INNER JOIN cao_os ON cao_fatura.co_os = cao_os.co_os WHERE cao_fatura.data_emissao >= '2007/1/1'  AND cao_fatura.data_emissao <= '2007/2/1' ");

$result=array();

while($fetchData=$queryResult->fetch_assoc()){
    $result[]=$fetchData;
}

echo json_encode($result);

?>

it works perfectly, i’d appreciate any help thank you =)

How to&Answers: