+ 3
Updating mysql date column become 0000-00-00 but with insert it work well
No update with date field success. All update made become 0000-00-00. Insert is working propery with the same value. Any help to fix or explanation will be admirable
6 ответов
+ 9
Additionally, to Calvin's excellent suggestion, date values are commonly wrapped in single quotes, in your query;
$query="UPDATE mytable SET start_date= '$s_date' WHERE Id=12";
+ 6
Show your code would me others understand your question.
+ 4
Great, now we can understand your issue, with the code showing.
Post value cannot be directly put into query as date value, you need to convert to sql date format.
Try
$s_date=date("Y-m-d H:i:s",strtotime($s_date));
Or
$s_date=date("Y-m-d",strtotime($s_date));
Depends the date is datetime or date format
+ 3
/* this is the full code*/
<?php
include ("connection.php");
if(isset($_post['btn-save'])){
$s_date=$_post['start_date'];
try{
$query="UPDATE mytable SET start_date= $s_date WHERE Id=12";
$stmt= $conn->prepare($query);
$stmt->execute();
}catch(PDOException $e){
echo $query." ".$e->getMessage();}
}
?>
//html form
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method= "post">
<input type="date" name="start_date" />
<button type="submit" name="btn-save">Save</button>
</form>
+ 3
Thanks for your intervention Mr. Calvin
+ 3
Thanks Ipang for your intervention, by surounding $s_date with quotes it works