I searched for this online and some posts even here on StackOverflow talk about this but never resolve to my problem. I am having a problem inserting DateTime into the database. The first problem is that I only know how to make StringRequests from java, so I convert date to String (output example: "2000-04-23 10:25:06").
This is my java code:
StringRequest stringRequest = new StringRequest(Request.Method.GET, testInserirPontoURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
}) {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
String codColab = getIntent().getStringExtra("codigo colaborador");
LocalDate ldHoraEntrada = LocalDate.of(ponto.getEntrada().get(Calendar.YEAR), ponto.getEntrada().get(Calendar.MONTH) + 1, ponto.getEntrada().get(Calendar.DAY_OF_MONTH));
String horaEntrada = ldHoraEntrada + " " + sdfHora2.format(ponto.getEntrada().getTime());
LocalDate ldSaidaAlmoco = LocalDate.of(ponto.getSaidaAlmoco().get(Calendar.YEAR), ponto.getSaidaAlmoco().get(Calendar.MONTH) + 1, ponto.getSaidaAlmoco().get(Calendar.DAY_OF_MONTH));
String saidaAlmoco = ldSaidaAlmoco + " " + sdfHora2.format(ponto.getSaidaAlmoco().getTime());
LocalDate ldEntradaTarde = LocalDate.of(ponto.getEntradaTarde().get(Calendar.YEAR), ponto.getEntradaTarde().get(Calendar.MONTH) + 1, ponto.getEntradaTarde().get(Calendar.DAY_OF_MONTH));
String entradaTarde = ldEntradaTarde + " " + sdfHora2.format(ponto.getEntradaTarde().getTime());
LocalDate ldHoraSaida = LocalDate.of(ponto.getSaida().get(Calendar.YEAR), ponto.getSaida().get(Calendar.MONTH) + 1, ponto.getSaida().get(Calendar.DAY_OF_MONTH));
String horaSaida = ldHoraSaida + " " + sdfHora2.format(ponto.getSaida().getTime());
Map<String, String> map = new HashMap<String, String>();
map.put("horaEntrada", horaEntrada);
map.put("saidaAlmoco", saidaAlmoco);
map.put("entradaTarde", entradaTarde);
map.put("horaSaida", horaSaida);
map.put("cod_colab", codColab);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(PontoActivity.this);
requestQueue.add(stringRequest);
note: The codColab
is a String in here but an int in the database, but it works just fine because I already use it for login.
This is my php code:
if($_SERVER['REQUEST_METHOD']=='GET'){
$horaEntrada = $_GET['horaEntrada'];
$saidaAlmoco = $_GET['saidaAlmoco'];
$entradaTarde = $_GET['entradaTarde'];
$horaSaida = $_GET['horaSaida'];
$cod_colab = $_GET['cod_colab'];
$sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('".$horaEntrada."', '".$saidaAlmoco."', '".$entradaTarde."', '".$horaSaida."', '".$cod_colab."')";
//$sql= "INSERT INTO `ponto` (`id`, `hora_entrada`, `saida_almoco`, `entrada_tarde`, `hora_saida`, `cod_colab`) VALUES (NULL, '2019-05-15 10:25:41', '2019-05-09 14:25:37', '2019-05-16 11:20:13', '2019-05-09 13:25:30', '1')";
//$sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('$horaEntrada', '$saidaAlmoco', '$entradaTarde', '$horaSaida', '$cod_colab')";
if (mysqli_query($conn, $sql)){
echo "registado";
} else {
echo "erro a registar";
}
Note: the comment lines are the ones I tried. the 1st comment line works but I want the values of the variables