I'd like to insert a date to my database. Unfortunately, I didn't get it to work using a variable. If I do it manually it works like a charm.
// doesn't work
$_POST['name'] = 'Tom';
$_POST['date'] = '2020-05-05';
$sql = "INSERT INTO tbl_user (name, date) VALUES (:name, :date)";
$stmt = $pdo->prepare($sql);
foreach ($_POST as $key => $data) {
$stmt->bindParam(':' . $key, $data);
}
// works
$_POST['name'] = 'Tom';
$sql = "INSERT INTO tbl_user (name, date) VALUES (:name, '2020-05-05')";
$stmt = $pdo->prepare($sql);
foreach ($_POST as $key => $data) {
$stmt->bindParam(':' . $key, $data);
}
PS: I don't get any errors and at the moment I add the values manually to $_POST. $key and $data matches perfectly. If I change my database column from DATE to VARCHAR(255) it works like a charm. So it has to do with the format of the date, doesn't it?