我想将日期保存到我的MySQL数据库,但是出现此错误:
user :: __ construct()必须是日期的实例,给出字符串
我尝试使用此行将输入类型date的结果转换为时间戳:
$myDate = date('Y-m-d', strtotime($_POST['birthdate']));
我得到了这个错误:
注意:未定义索引:2020-11-11
user :: __ construct()必须是日期的实例,给定null
当我尝试回显变量$ myDate的数据类型时,它给了我String。
这是我的代码:
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:我正在使用PHP 7.4,所以这是我的构造函数:
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;
}
(当我将类型更改为字符串,将输入类型更改为文本时,它工作正常,因此我认为问题不在我的查询范围之内。)
这是我将时间戳记传递给对象时的屏幕截图
我输入类型输入日期时显示此屏幕
答案 0 :(得分:-1)
尝试做
$myDate = date("Y-m-d H:i:s", strtotime($_POST['birthdate']));
代替
$myDate = date('Y-m-d', strtotime($_POST['birthdate']));