gpt4 book ai didi

PHP 从表单中插入日期时间

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:41 26 4
gpt4 key购买 nike

我有一个表单,用户需要在其中选择两个日期,一个只是日期,一个是日期和时间。我有一个包含两个字段的基本表单,用户必须在其中输入日期,如 2014-02-02 和时间 datetime,如 2014-02-02 10:20:00,表单验证非常有效。

然而,当将值插入数据库时​​,问题就来了。这是我的:

<?php
class Quote
{
public $job_deadline = null;
public $job_dispatchdate = null;
}

public function __construct( $data=array() ) {
if ( isset( $data['job_deadline'] ) ) $this->job_deadline = (int) $data['job_deadline'];
if ( isset( $data['job_dispatchdate'] ) ) $this->job_dispatchdate = (int) $data['job_dispatchdate'];
//if ( isset( $data['job_dispatchdate'] ) ) $this->job_dispatchdate = date('Y-m-d H:i:s',strtotime($data['job_dispatchdate']));
}

public function storeFormValues ( $params ) {
$this->__construct( $params );

if ( isset($params['job_deadline']) ) {
$job_deadline = explode ( '-', $params['job_deadline'] );

if ( count($job_deadline) == 3 ) {
list ( $y, $m, $d ) = $job_deadline;
$this->job_deadline = gmmktime ( 0, 0, 0, $m, $d, $y );
}
}

if ( isset($params['job_dispatchdate']) ) {
$job_deadline = $params['job_dispatchdate'];
list ( $y, $m, $d, $h, $i, $s ) = $job_dispatchdate;
$this->job_dispatchdate = gmmktime ( 0, 0, 0, 0, 0, 0, $y, $m, $d, $h, $i, $s );
}

/*
if ( isset($params['job_dispatchdate']) ) {
$datetime = date('Y-m-d H:i:s', strtotime($params['job_dispatchdate']));
}
*/
}

public function insertjob() {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

//$datetime = date('Y-m-d H:i:s', strtotime($params['job_dispatchdate'])); USING $datetime inplace of FROM_UNIXTIME(:job_dispatchdate) and removing st for job_dispatchdate

$sql = "INSERT INTO tbl1 (job_deadline, job_dispatchdate)
VALUES (FROM_UNIXTIME(:job_deadline), FROM_UNIXTIME(:job_dispatchdate))";
$st = $conn->prepare ( $sql );
$st->bindValue( ":job_deadline", $this->job_deadline, PDO::PARAM_INT );
$st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_INT );
$st->execute();
$this->job_id = $conn->lastInsertId();
$inserted_id = $this->id = $conn->lastInsertId();
$conn = null;
}
?>

job_deadline 插入正常,问题出在 job_dispatchdate。注释掉的部分是我尝试过的,但也是不同的工作。我从来没有得到任何插入,所以该字段显示为 NULL 或像 1970-01-01 00:00:00 这样的日期。

谁能帮我插入日期时间。

提前致谢。

伊恩

---编辑---

<?php
class Quote
{
public $job_deadline = null;
public $job_dispatchdate = null;
}

public function __construct( $data=array() ) {
if ( isset( $data['job_deadline'] ) ) $this->job_deadline = (int) $data['job_deadline'];
if ( isset( $data['job_dispatchdate'] ) ) $this->job_dispatchdate = preg_replace ( "/[^\.\,\-\_\'\|\+\#\"\@\%\?\!\&\:\;\£\$\/\\\(\n) a-zA-Z0-9()]/", "", $data['job_dispatchdate'] );
}

public function storeFormValues ( $params ) {
$this->__construct( $params );

if ( isset($params['job_deadline']) ) {
$job_deadline = explode ( '-', $params['job_deadline'] );

if ( count($job_deadline) == 3 ) {
list ( $y, $m, $d ) = $job_deadline;
$this->job_deadline = gmmktime ( 0, 0, 0, $m, $d, $y );
}
}

if ( isset($params['job_dispatchdate']) ) {
$job_deadline = $params['job_dispatchdate'];
list ( $y, $m, $d, $h, $i, $s ) = $job_dispatchdate;
$this->job_dispatchdate = gmmktime ( 0, 0, 0, 0, 0, 0, $y, $m, $d, $h, $i, $s );
}
}

public function insertjob() {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
//$datetime = date('Y-m-d H:i:s', strtotime($params['job_dispatchdate'])); USING $datetime inplace of FROM_UNIXTIME(:job_dispatchdate) and removing st for job_dispatchdate
$sql = "INSERT INTO tbl1 (job_deadline, job_dispatchdate)
VALUES (FROM_UNIXTIME(:job_deadline), FROM_UNIXTIME(:job_dispatchdate))";
$st = $conn->prepare ( $sql );
$st->bindValue( ":job_deadline", $this->job_deadline, PDO::PARAM_INT );
$st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_STR );
$st->execute();
$this->job_id = $conn->lastInsertId();
$inserted_id = $this->id = $conn->lastInsertId();
$conn = null;
}
?>

根据评论,我已将代码更改为此。并将数据库中的字段设置为字符串 Varchar 但无济于事。

最佳答案

 $st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_INT)

应该是

 $st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_STR)

建议:最好将所有日期以 unix 时间戳格式存储在 mysql 中。

EDIT 2 :将您的 job_dispatche 列类型更改为 STRING 如果它是 INT 类型

关于PHP 从表单中插入日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908163/

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