Problems saving PHP date

0

I have a project on the web and I am trying to save the complete date in the database but it is giving me a strange error, I do not understand what the error is.

Note: Recently everything was going well because in my database in the field where I saved the date was type DATE and I realized that I need the full date with hours and minutes, then I changed the format to DATETIME after I changed it also changed the function of the date, here you will see an example

$fecha=date('Y-m-d');//antes 
$fecha=date('Y-m-d H:i:s');//nueva

I clarify that I am working with prepared queries and this is the way I insert the information

$query=$con->prepare("INSERT INTO noticias (titulo,fecha)VALUES(?,?)");
$sql->bind_param("sd", $titulo,$fecha);
if($sql->execute()){

  $sql->close();
    echo"exito";
}else{

   echo"error ".mysqli_error($con)
}

The error that throws me is the following

  

Error: Incorrect datetime value: '2018' for column 'date' at row 1

    
asked by andy gibbs 19.12.2018 в 18:42
source

3 answers

4

If we review the documentation of msqli::bind_param() we see that there are four characters for Define the type of variable:

+---+---------------------------------------------------------------+
| i | la variable correspondiente es de tipo entero                 |
+---+---------------------------------------------------------------+
| d | la variable correspondiente es de tipo double                 |
+---+---------------------------------------------------------------+
| s | la variable correspondiente es de tipo string                 |
+---+---------------------------------------------------------------+
| b | la variable correspondiente es un blob y se envía en paquetes |
+---+---------------------------------------------------------------+

In this case, the dates must be passed as a string ( s ) since the rest of the types do not fit, currently you indicate it is double type, check:

$sql->bind_param("sd", $titulo,$fecha);

You also need a semicolon in:

   echo "error " . mysqli_error($con)

As well as some spaces that would improve the reading of the code in general

Therefore your code should look like this:

//$fecha=date('Y-m-d');//antes 
$fecha = date('Y-m-d H:i:s'); //nueva

$query = $con->prepare("INSERT INTO noticias (titulo, fecha) VALUES (?, ?)");
// indicamos que la variable titulo y fecha son de tipo string
$sql->bind_param("ss", $titulo, $fecha);

if($sql->execute()) {
    $sql->close();
    echo "exito";
} else {
    echo "error " . mysqli_error($con);
}
    
answered by 19.12.2018 / 19:29
source
1

If what you want is to pass the system date (which is what I see you are doing) rather than passing a defined date (for a post field) what you can do is send the query directly to obtain the corresponding format for the database ie as follows

$query=$con->prepare("INSERT INTO noticias (titulo,fecha)VALUES(?,?)");
$sql->bind_param("sd", $titulo,"CURRENT_TIMESTAMP()");
if($sql->execute()){

  $sql->close();
    echo"exito";
}else{

   echo"error ".mysqli_error($con)
}

what will be done is the following

INSERT INTO noticias (titulo, fecha) VALUES ('titulo', CURRENT_TIMESTAMP())

and what mysql will do is insert the data corresponding to the date and time of the transaction

reference ( link )

    
answered by 19.12.2018 в 19:50
1

You are passing the date as a double and must be string, you must change:

$sql->bind_param("sd", $titulo,$fecha);

by:

$sql->bind_param("ss", $titulo,$fecha);
    
answered by 19.12.2018 в 20:28