0
votes

I want to save a date to my MySQL database but I get this error:

user::__construct() must be an instance of date, string given

I tried to convert the result of my input type date to a timestamp using this line:

$myDate = date('Y-m-d', strtotime($_POST['birthdate']));

And I got this error:

Notice: Undefined index: 2020-11-11
user::__construct() must be an instance of date, null given

When I try to echo the data type of the variable $myDate it gives me String.

Here's my code:

if (isset($_POST['name']) and isset($_POST['surname']) and 
isset($_POST['birthdate']) and isset($_POST['email']) 
and isset($_POST['password'])){

//$myDate = date('Y-m-d', strtotime($_POST['birthdate']));
//echo $myDate;
//echo gettype($myDate);

$user1= new 
user($_POST['name'],$_POST['surname'],$_POST['birthdate'],$_POST['email'],$_POST['password']);
$userCores= new userCores();
$userCores->addUser($user1);

//header('Location: showUser.php');

}
else 
{
echo "Missing fields" ; 
}

HTML:

              <form name="RegisterForm" method="POST" action="register.php">
            <div class="form-group">
              <label for="nom">Nom <sup class="text-danger">*</sup></label>
              <input type="text" class="form-control" id="name" name="name"  placeholder="Entrer 
            votre nom" >
            </div>
            <div class="form-group">
              <label for="prenom">Prenom <sup class="text-danger">*</sup></label>
              <input type="text" class="form-control" id="surname" name="surname"  
            placeholder="Entrer votre prenom" >
            </div>
            <div class="form-group">
              <label for="birthDate">date de naissance <sup class="text-danger">*</sup></label>
              <input type="date" class="form-control" id="birthdate" name="birthdate"  
            placeholder="Entrer votre date de naissance" >
            </div>
            <div class="form-group">
              <label for="email">Email <sup class="text-danger">*</sup></label>
              <input type="email" class="form-control" id="email" name="email"  placeholder="Entrer 
            votre email" >
            </div>
            <div class="form-group">
              <label for="password">Mot de passe <sup class="text-danger">*</sup></label>
              <input type="password" class="form-control" id="password" name="password" 
             placeholder="Mot de passe" >
            </div>
            <button  type="submit" class="btn btn-primary-color w-100"  >Envoyer</button>
            
          </form>

PS: I'm using PHP 7.4 so here's my constructor:

private String $id;
private String $name;
private String $surname;
private date $birthdate;
private String $email;
private String $password;


function __construct(String $name, String $surname,date $birthdate, String $email,String $password){
    $this->name = $name;
    $this->surname = $surname;
    $this->birthdate = $birthdate;
    $this->email = $email;
    $this->password = $password;
}

(When I change the type to string and input type to text it works fine so I don't think that the problem is in my query).

Here's a screenshot when I pass a timestamp to the object

enter image description here

This screen when I pass the input type date

enter image description here

1
please show the content $_POST['birthdate']nbk
It shows the date that i inserted in the input name="birthdate"Amine Missaoui
are you sure, please verify it, i would have guessed that it doesn't existnbk
See the screenshot in my postAmine Missaoui
the screenshot tells you clearly that it gets NULL instead if a date, as i already saudnbk

1 Answers

-1
votes

try doing

$myDate = date("Y-m-d H:i:s", strtotime($_POST['birthdate'])); 

instead of

$myDate = date('Y-m-d', strtotime($_POST['birthdate']));