我未能将日期值插入到数据库中。它总是显示我"0000-00-00"
,我设定的日期结构是日期,它是一个"yyyy-mm-dd"
。我试图调试我的查询,除了日期值不能插入到数据库中,一切都可以。我的代码有错吗?无法使用php将日期插入到数据库中
这里是我的代码:
$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';}
}
的var_dump($ newFrom); – underscore
数据库中“startdate”字段的类型是什么? –
由于您正在处理字符串,因此将您的变量放在引号'SET startdate ='$ newFrom''中。 –