Php SQL插入日期始终为空
我正在尝试使用上载页面插入数据库,代码如下:Php SQL插入日期始终为空,php,mysql,sql,csv,Php,Mysql,Sql,Csv,我正在尝试使用上载页面插入数据库,代码如下: if($file!=="") { echo "<br/>".$file; $handle = fopen($file, "r"); $row = 0; $delete_records = mysql_query("DELETE FROM affiliationagreements"); $delete_records = mysql_query("
if($file!=="")
{
echo "<br/>".$file;
$handle = fopen($file, "r");
$row = 0;
$delete_records = mysql_query("DELETE FROM affiliationagreements");
$delete_records = mysql_query("DELETE FROM college");
$delete_records = mysql_query("DELETE FROM program");
$delete_records = mysql_query("DELETE FROM facility");
$delete_records = mysql_query("DELETE FROM submitor");
$delete_records = mysql_query("DELETE FROM location");
//will loop each record in the CSV file
while(($fileop = fgetcsv($handle,1000,",")) !== false )
{
//columns names of the CSV file are not needed
if($row==0)
{
$row++;
}
else
{
//accept apostrophes in the strings
$fileop = array_map("mysql_real_escape_string",$fileop);
$sql = mysql_query("INSERT INTO affiliationagreements(id, AANumber, Facility, Submitor, Program, Location, College, SubmissionDate, Action, EffectiveDate, Status, ExpirationDate)
VALUES('',
'$fileop[0]',
'$fileop[1]',
'$fileop[2]',
'$fileop[3]',
'$fileop[4]',
'$fileop[5]',
'$fileop[11]',
'$fileop[23]',
'$fileop[24]',
'$fileop[25]',
'$fileop[26]')
")or die(mysql_error());
如果我将SubmissionDate、EffectiveDate和ExpirationDate更改为varchar,它们将正确插入,但我无法使用varchar,因为我正在比较日期值。还有建议
***更新。在CSV文件中,格式为MM/DD/YYYY。我不认为这会是个问题。改变这个会更好吗?我删除记录是因为我的老板希望在重新上传文件之前清除数据库,因为上传的文件是之前上传的文件的更新**** 检查日期格式,因为它需要是YYYY-MM-DD格式
INSERT INTO table SET date = '2014-05-13'
如果格式不同,则日期将改为存储“0000-00-00”。因此,在运行查询之前,通过回显查询字符串来仔细检查插入情况。mysql
date
字段仅接受以下格式的日期:0000-00-00
您可以在insert查询中使用DATE\u FORMAT()
,但我不能给出示例,因为您没有公布实际日期的格式
旁注:您确实意识到您一直在用
$delete\u records=mysql\u query
覆盖您的查询,另外,您没有WHERE
子句,那么如何知道删除什么呢?@Fred ii-这是故意的,因为它是一个测试脚本。此外,由于性能原因,不建议使用NULL。您是否尝试回显您的查询?您将不得不发布一些有效负载数据。当你猜不到你要插入的实际值时,很好。如果这是问题所在,我不会感到惊讶。是的,就是这样。愚蠢的数据库简直是荒谬可笑。再次感谢
INSERT INTO table SET date = '2014-05-13'