I need to capture multiple date visits that a client makes at once from an HTML form. All the other data types get stored successfully but the date is changed into the default “1970-01-01“.
Is there a way of converting the date array into individual dates as picked from the HTML Form to avoid the default dates and the error: “Undefined index: 01-01-1970“.
I have tried to play around with the date array but no luck.
<?php //Post Variables //name="ClientID[]" 1 //name="ClientID[]" 2 //$ClientID = array(1,2); //name="VisitDate[]" 01-01-2021 //name="VisitDate[]" 01-01-2020 //$VisitDate=array('01-01-2021','01-01-2022'); $ClientID=$_POST['ClientID']; $VisitDate=$_POST['VisitDate']; foreach ($VisitDate as $k=>$v) { $VisitDate[$k] = date('Y-m-d', strtotime($v)); } for ($i = 0; $i < count($ClientID); $i++) { $ClientID=$ClientID[$i]; $VisitDate=$VisitDate[$i]; } $query=$con->prepare("insert into records (ClientID,VisitDate) values ('$ClientID','$VisitDate')"); $result=$query->execute(); ?>
Advertisement
Answer
Dates are difficult to manage because MySQL checks them through. For now just change your datatype from your Database to varchar and drop the date conversion process. Since you are using a datepicker, the user will not enter date in a wrong format much as it will be identified as varchar in the database.
<?php //Post Variables //name="ClientID[]" 1 //name="ClientID[]" 2 //$ClientID = array(1,2); //name="VisitDate[]" 01-01-2021 //name="VisitDate[]" 01-01-2020 //$VisitDate=array('01-01-2021','01-01-2022'); $ClientID=$_POST['ClientID']; $VisitDate=$_POST['VisitDate']; //Deleted the date array conversion for ($i = 0; $i < count($ClientID); $i++) { $ClientID=$ClientID[$i]; $VisitDate=$VisitDate[$i]; } $query=$con->prepare("insert into records (ClientID,VisitDate) values ('$ClientID','$VisitDate')"); $result=$query->execute(); ?>
This should work.