gpt4 book ai didi

php - 通过 PHP 插入 MySQL 时日期时间格式无效

转载 作者:行者123 更新时间:2023-11-29 18:25:10 24 4
gpt4 key购买 nike

我尝试插入日期和时间,但显示错误

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect time value: '1505572990' for column 'vdate' at row 1

请帮助我编辑代码并解决问题

我的代码:

class.php

<?php
require_once 'db.php';
require_once 'lang.php';

class USER
{

private $conn;

public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}

public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}

public function lasdID()
{
$stmt = $this->conn->lastInsertId();
return $stmt;
}

public function register($uname)
{
try
{

$stmt = $this->conn->prepare("INSERT INTO validation(vdate)
VALUES(:user_name)");
$stmt->bindparam(":user_name",$uname);
$stmt->execute();
return $stmt;
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
}

index.php

<?php
session_start();
require_once 'class.php';
$reg_user = new USER();

if(isset($_POST['btn-signup']))
{
$uname = time();


if($reg_user->register($uname))
{
$msg = "
<div class='alert alert-success'>
<button class='close' data-dismiss='alert'>&times;</button>
<strong>Success!</strong> To activate your account, you need to verify your email. We have sent a verify link at <strong></strong>.
</div>
";
}

else
{
echo "sorry , Query could no execute...";
}
}
?>

我唯一想要的是,当用户单击按钮时,它应该插入 date&time在 MySQL 中。

我尝试了上面的代码,但它显示错误

SQLSTATE[22007]: Invalid DateTime format: 1292 Incorrect time value: '1505572990' for column 'vdate' at row 1

请帮助我编辑代码并解决问题

最佳答案

如果“vdate”列的类型是DATETIME,则需要在插入之前进行转换

<?php $vdate = date("Y-m-d H:i:s", time()) ?>

关于php - 通过 PHP 插入 MySQL 时日期时间格式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46255597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com