-1

i'm student of PHP, and i build an app in android that save any data in Mysql BD, but i have problem with TIMESTAMP, that it saving date like 0000-00-00 00:00:00, below i attach my php code.

I tested with another php file, and the android app, send date correctly

PHP code

$db = new DbOperation();

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){ 
    //crear datos tabla registro
    require 'php_tracker_server/conn.php';
    registro();

    //hecking the required params 
    if(isset($_POST['title']) and isset($_POST['message']) and isset($_POST['r_cel'])){

        //creating a new push
        $push = null; 
        //first check if the push has an image with it
        if(isset($_POST['image'])){
            $push = new Push(
                    $_POST['title'],
                    $_POST['message'],
                    $_POST['image']
                );
        }else{
            //if the push don't have an image give null in place of image
            $push = new Push(
                    $_POST['title'],
                    $_POST['message'],
                    null
                );
        }

        //getting the push from push object
        $mPushNotification = $push->getPush(); 

        //getting the token from database object 
        $devicetoken = $db->getTokenByr_cel($_POST['r_cel']);

        //creating firebase class object 
        $firebase = new Firebase(); 

        //sending push notification and displaying result 
        echo $firebase->send($devicetoken, $mPushNotification);
    }else{
        $response['error']=true;
        $response['message']='Parameters missing';
    }
}else{
    $response['error']=true;
    $response['message']='Invalid request';
}

echo json_encode($response);

function registro() {
    global $connect;
    $cel = $_POST[ "r_cel" ];
    $titulo = $_POST[ "title" ];
    $fecha_r = $_POST[ "date" ];
    $msj = $_POST[ "message" ];


    //$query = "update escolar_tracker set latitude='$latitude',longitude='$longitude',date='$date',estado='$estado',speed='$speed' where ruta='$ruta' and jornada='$jornada'";
    $query = "Insert into table1 (cel, titulo, fecha_r, msj) VALUES ('$cel','$titulo','$fecha_r','$msj')";

    mysqli_query( $connect, $query )or die( mysqli_error( $connect ) );
    mysqli_close( $connect );
    }
Andres
  • 11
  • 1
  • 1
    How is `$fecha_r` formatted? – hungrykoala Jul 06 '18 at 02:53
  • Questions seeking debugging help (“**why isn’t this code working?**”) must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a ***Minimal***, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – elixenide Jul 06 '18 at 02:54
  • Also, you are wide open to [**SQL injection**](https://www.owasp.org/index.php/SQL_Injection). You need to use prepared statements, rather than concatenating variables into your query. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). – elixenide Jul 06 '18 at 02:54

1 Answers1

0

First of all, I agree with Ed Cottrell in terms of the question formatting and the SQL injection.

Secondly, how is the field in mysql configured? You have to set a default value for Timestamp or Date time to automatically update when you didn't pass a value.

More info here: https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html

dpattayath
  • 160
  • 5