how to insert date values in mysql table

0

I have a problem inserting dates in a table column mysql .

I've already tried putting the field as varchar , date , datetime and nothing.

$fecha_ant = date("Y-m-d", strtotime("-3 day")); // with this I get the current date - 3 days, it returns the correct date

My insert

$conn->exec("INSERT INTO tVentas (vendedor_id,fecha_trans,amount,tienda) VALUES($id_vendedor,$fecha_ant,$monto,0)");

When you want to insert this data in mysql if the field of mysql is as date does not insert anything and if it is as varchar instead of inserting the date, insert a number.

For example if the extracted date is "2016-11-18", insert "1987".

Reading about it, it may be something from the unix date, but I'm not sure. In DB I want to insert 2016-11-18 and not 1987

    
asked by Doenitz Galán 21.11.2016 в 16:37
source

1 answer

4

It seems that you are trying to insert the date without quotes that escape the value.

$conn->exec("INSERT INTO tVentas (vendedor_id,fecha_trans,amount,tienda) VALUES($id_vendedor,$fecha_ant,$monto,0)");

You should try to create the query in the following way

$conn->exec("INSERT INTO tVentas (vendedor_id,fecha_trans,amount,tienda) VALUES($id_vendedor,'$fecha_ant', $monto, 0)");

Anyway I recommend you use PDO to ensure the queries you make to the database.

$stmt = $pdo->prepare('INSERT INTO tVentas (vendedor_id,fecha_trans,amount,tienda) VALUES(?, ?, ?, ?)');
$stmt->execute([$id_vendedor, $fecha_ant, $monto, 0]);
    
answered by 21.11.2016 / 16:43
source