无法使用php将日期插入数据库

时间:2014-04-26 15:53:28

标签: php mysql sql date

我无法将日期值插入数据库。它始终显示我"0000-00-00",我设置的日期结构是日期,它是"yyyy-mm-dd"。我曾尝试调试我的查询,一切正常,除了日期值无法插入数据库。我的代码是否有任何错误?

这是我的代码:

$from = $_POST['from'];

$newFrom = date("Y-m-d",strtotime($from));
// Get default database object
$db = JFactory::getDBO();

// Get a new JDatabaseQuery object
$query = $db->getQuery(true);

$tmpIds = array();

foreach($courseID as $cId) {
    $tmpIds[] = $db->quote($cId);
    //sanitize the input
}

$courseID1 = implode(',',$tmpIds);

// Build the query
$query->select($db->quoteName('courseid'));
$query->from($db->quoteName('intake'));
$query->where($db->quoteName('campusid').'='. $db->quote($campusID));

$query->where($db->quoteName('courseid').'IN('.$courseID1.')');

// Set the query for the DB oject to execute
$db->setQuery($query);
// Get the DB object to load the results as a list of objects
$results = $db->loadObjectList(); 

if($results){

    $query = "UPDATE intake SET startdate = $newFrom WHERE campusid = '$campusID'";
    echo "$query";
    $db->setQuery( $query );
    $db->query();

}

else{ echo 'Error';}

}

1 个答案:

答案 0 :(得分:3)

您的日期字符串周围缺少引号:

$query = "UPDATE intake SET startdate = $newFrom WHERE campusid = '$campusID'";

应该是:

$query = "UPDATE intake SET startdate = '$newFrom' WHERE campusid = '$campusID'";