0

I'm using query INSERT INTO but I found this message in Network/preview (chrome)

Error: INSERT INTO indisponible (id_formateur,date_debut,date_fin) VALUES ( 0, 2017-04-06 , 2017-04-13)
Incorrect date value: '2007' for column 'date_debut' at row 1

this is my code php:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-type');

 $postdata = file_get_contents("php://input"); 
    $request = json_decode($postdata); 
    $id = $request->id;
    $debut = date('Y-m-d', strtotime($request->debut));
    $fin = date('Y-m-d', strtotime($request->fin));


$servername = "localhost"; 
$username = "root";
$password = ""; 
$dbname = "planni";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO indisponible (id_formateur,date_debut,date_fin)
VALUES ( $id, $debut , $fin)";
$conn->query($sql);

if ($conn->query($sql) === TRUE) {
    echo "ajouté avec succé !";
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
Fredj
  • 59
  • 2
  • 9
  • 2
    Strings needs to be quoted. `VALUES ( $id, $debut , $fin)";` should be `VALUES ( $id, '$debut' , '$fin')";` - or just learn how to use parameterized queries, and you'll never have that problem. – Qirel Apr 05 '17 at 12:38
  • 1
    Get rid of the SQL injection vulnerability and the problem becomes moot. SQL prepared statements and query parameters aren't just about security, they're also about stable and maintainable code. – David Apr 05 '17 at 12:40
  • it's working now like this: VALUES ( $id, '$debut' , '$fin')"; – Fredj Apr 05 '17 at 12:46

0 Answers0