我有一个表单,用户需要选择两个日期,一个是日期,一个是日期和时间。我有两个字段的基本表单,其中用户必须输入2014-02-02
之类的日期和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
。
任何人都可以帮我插入日期时间。
提前致谢。
伊恩
--- --- EDIT
<?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;
}
?>
根据评论,我已将代码更改为此。并将DB中的字段设置为字符串Varchar但无效。
答案 0 :(得分:0)
$st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_INT)
应该是
$st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_STR)
建议:最好以unix时间戳格式将所有日期存储在mysql中。
编辑2 :如果 INT 类型
,则将您的job_dispatche列类型更改为 STRING答案 1 :(得分:0)
if ( isset($params['job_dispatchdate']) ) {
$job_deadline = $params['job_dispatchdate'];
$this->job_dispatchdate = date('Y-m-d H:i:s', strtotime( $job_deadline ));
}
这似乎解决了这个问题。谢谢大家