1

I am reading the date from android app and send it (As string) to PHP web service to insert the time to MySQL database and save it in string fields but this is not happening.The time format is DD-MM-YYYY like that 25-04-2017, Do I need to convert the date to another format. Here is my PHP code:

<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
       require "init.php";

       creat_Student();
}
function creat_Student()
{
    global $con;  
    $user=$_POST["user_id"];
    $car=$_POST["car_id"];
    $dating=$_POST["dating"];
   ;     
    $query="Insert Into items (user_id,car_id,dating) values ('$user','$car','$dating');";
    if(mysqli_query($con,$query))
    echo $con->insert_id;
    else
      echo "Error";   
     mysqli_close($con);
}
?> 

enter image description here

Saleh Refaai
  • 689
  • 3
  • 14
  • 25
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 26 '17 at 19:24
  • Please share with us the values of the added row, I mean the end result in the DB table. – Ofir Baruch Apr 26 '17 at 19:24
  • It depends what the date column structure is formatted to accept. Share the mysql schema – Andrew Rayner Apr 26 '17 at 19:30
  • I added pic of database – Saleh Refaai Apr 26 '17 at 19:48

1 Answers1

2

Depending on your database column schema, generally you can string to time $dating then format as YYYY-MM-DD H:i:s or YYYY-MM-DD

DATETIME in DB Column

$dating = date('Y-m-d H:i:s',strototime($dating));

OR

DATE in DB Column

$dating = date('Y-m-d',strototime($dating));
clearshot66
  • 2,292
  • 1
  • 8
  • 17