我要把csv格式的文件数据导入到相应的表中、为什么老是提示 员工未注册、我测试了下 好像是 ueser_name 没有取到值 请各位大侠指教<?php
include_once( "inc/auth.php" );
include_once( "inc/utility_all.php" );
echo "<html>\r\n<head>\r\n<title>导入数据</title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\r\n</head>\r\n<body class=\"bodycolor\" topmargin=\"5\">\r\n";
if ( $FILE_NAME == "" )
{
$query = "SELECT count(*) from SAL_DATA where FLOW_ID='".$FLOW_ID."'";
$cursor = exequery( $connection, $query );
if ( $ROW = mysql_fetch_array( $cursor ) )
{
$ROW_COUNT = $ROW[0];
}
echo "<script Language=\"JavaScript\">\r\nfunction CheckForm()\r\n{\r\n   if(document.form1.CSV_FILE.value==\"\")\r\n   { alert(\"请选择要导入的文件!\");\r\n     return (false);\r\n   }\r\n\r\n   if (document.form1.CSV_FILE.value!=\"\")\r\n   {\r\n     var file_temp=document.form1.CSV_FILE.value,file_name;\r\n     var Pos;\r\n     Pos=file_temp.lastIndexOf(\"\\\\\");\r\n     file_name=file_temp.substring(Pos+1,file_temp.length);\r\n     document.form1.FILE_NAME.value=file_name;\r\n   }\r\n\r\n   return (true);\r\n}\r\n</script>\r\n\r\n  <table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"3\" class=\"small\">\r\n    <tr>\r\n";
if ( $ROW_COUNT != 0 )
{
echo "      <td class=\"Big\"><img src=\"/images/sys_config.gif\" align=\"absmiddle\"><span class=\"big3\"> 导入CSV工资数据(该流程已录入过数据)</span><br>\r\n      </td>\r\n";
}
else
{
echo "      <td class=\"Big\"><img src=\"/images/sys_config.gif\" align=\"absmiddle\"><span class=\"big3\"> 导入CSV工资数据</span><br>\r\n      </td>\r\n";
}
echo "    </tr>\r\n  </table>\r\n\r\n  <br>\r\n  <br>\r\n\r\n  <div align=\"center\" class=\"Big1\">\r\n  <b>请指定用于导入的CSV文件:</b>\r\n  <form name=\"form1\" method=\"post\" action=\"import.php?FLOW_ID=";
echo $FLOW_ID;
echo "\" enctype=\"multipart/form-data\" onSubmit=\"return CheckForm();\">\r\n    <input type=\"file\" name=\"CSV_FILE\" class=\"BigInput\" size=\"30\">\r\n    <input type=\"hidden\" name=\"FILE_NAME\">\r\n    <input type=\"hidden\" name=\"GROUP_ID\" value=\"";
echo $GROUP_ID;
echo "\">\r\n    <input type=\"submit\" value=\"导入\" class=\"BigButton\">\r\n  </form>\r\n\r\n  <br>\r\n  <input type=\"button\" value=\"返回\" class=\"BigButton\" onClick=\"location='index.php'\">\r\n  ";
if ( $FILE_NAME == "" )
{
message( "说明", "1)EXECL的工资报表的列顺序为姓名、工资项目,将部门、职务等列删除;<BR>2)将改好的EXECL工资报表另存为CSV格式的文件;" );
}
echo "\r\n  </div>\r\n";
exit( );
}
if ( strtolower( substr( $FILE_NAME, -3 ) ) != "csv" )
{
message( "错误", "只能导入CSV文件!" );
button_back( );
exit( );
}
$query = "SELECT ITEM_ID ,ITEM_NAME from SAL_ITEM";
$cursor = exequery( $connection, $query );
$title = array( );
while ( $ROW = mysql_fetch_array( $cursor ) )
{
$ITEM_NAME = $ROW['ITEM_NAME'];
$ITEM_ID = "S".$ROW['ITEM_ID'];
$title[$ITEM_NAME] = $ITEM_ID;
}
$title += array( "姓名" => "USER_NAME" );
$ROW_COUNT = 0;
$data = file_get_contents( $CSV_FILE );
$lines = csv2array( $data, $title, "\t" );
if ( !$data )
{
message( "错误", "打开文件错误!" );
button_back( );
exit( );
}
$query = "DELETE FROM `SAL_DATA` WHERE `FLOW_ID` = '".$FLOW_ID."'";
$cursor = exequery( $connection, $query );
foreach ( $lines as $line )
{
$STR_VALUE = "";
$STR_KEY = "";
foreach ( $line as $key => $value )
{
if ( $key != "USER_NAME" )
{
$STR_KEY .= $key.",";
$STR_VALUE .= "'".$value."',";
}
else
{
$USER_NAME = $value;
}
}
if ( substr( $STR_KEY, -1 ) == "," )
{
$STR_KEY = substr( $STR_KEY, 0, -1 );
}
if ( substr( $STR_VALUE, -1 ) == "," )
{
$STR_VALUE = substr( $STR_VALUE, 0, -1 );
}
$query = "insert into SAL_DATA(FLOW_ID,USER_ID,";
$query1 = "SELECT USER_ID from USER where USER_NAME='".$USER_NAME."'";
$cursor1 = exequery( $connection, $query1 );
if ( !( $ROW = mysql_fetch_array( $cursor1 ) ) )
{
echo "<font color=#FF6633><b>员工".$USER_NAME."尚未在OA系统中注册!!</b></font><br>";
}
else
{
$query .= $STR_KEY.( ") values ('".$FLOW_ID."','{$ROW['0']}'," ).$STR_VALUE.")";
exequery( $connection, $query );
echo "员工".$USER_NAME."的工资导入完成!!<br>";
++$ROW_COUNT;
}
}
if ( file_exists( $CSV_FILE ) )
{
@unlink( $CSV_FILE );
}
message( "", "共".$ROW_COUNT."条数据导入!" );
echo "<div align=\"center\">\r\n<input type=\"button\" value=\"返回\" class=\"BigButton\" onClick=\"location='index.php';\" title=\"返回\">\r\n</div>\r\n\r\n</body>\r\n</html>\r\n";
?>

解决方案 »

  1.   

    鬼才知道你在做什么
    你连 csv 的样式和读取方法都多不给出?
      

  2.   

    大哥  我csv的格式和我数据库的字段名称对应起来的。 关键是代码中哪个地方错了,  user_name好像没有取到值,  哪位大侠 帮忙看看   留qq或者加我qq:271663858 联机帮我看下
      

  3.   

    csv 格式 就是 EXECL的工资报表的列顺序为姓名、工资项目          
      

  4.   

    读取方法如下:  由于工资项目是可以调整的、所以title 的 array() 没有写死。
    function CSV2Array( $content, $title = array( ), $delimiter = ",", $enclosure = "\"", $optional = 1 )
    {
    $content = trim( $content );
    $content = str_replace( "\r", "", $content );
    $reg = "/((".$enclosure.")".( $optional ? "?(?(2)" : "(" )."[^".$enclosure."]*".$enclosure."|[^".$delimiter."\\n]*))(".$delimiter."|\\n)/smi";
    preg_match_all( $reg, $content, $treffer );
    $linecount = 0;
    $i = 0;
    for ( ; $i <= count( $treffer[3] ); ++$i )
    {
    $field = $treffer[1][$i];
    if ( $field[0] == $enclosure && $field[strlen( $field ) - 1] == $enclosure )
    {
    $field = substr( $field, 1, -1 );
    }
    $liste[$linecount][] = str_replace( $enclosure.$enclosure, $enclosure, $field );
    if ( $treffer[3][$i] != $delimiter )
    {
    ++$linecount;
    }
    }
    if ( !is_array( $title ) && count( $title ) == 0 || count( $liste ) == 0 )
    {
    return $liste;
    }
    $field_map = array( );
    while ( list( $key, $value ) = each( &$title ) )
    {
    if ( ( $index = array_search( $key, $liste[0] ) ) !== FALSE )
    {
    $field_map[$value] = $index;
    }
    }
    $lines = array( );
    $i = 1;
    for ( ; $i < $linecount; ++$i )
    {
    $line = array( );
    reset( &$field_map );
    while ( list( $key, $value ) = each( &$field_map ) )
    {
    $line[$key] = $liste[$i][$value];
    }
    $lines[] = $line;
    }
    return $lines;
    }
      

  5.   


    $query = "insert into SAL_DATA(FLOW_ID,USER_ID,";
    $query1 = "SELECT USER_ID from USER where USER_NAME='".$USER_NAME."'";
    $cursor1 = exequery( $connection, $query1 );
    if ( !( $ROW = mysql_fetch_array( $cursor1 ) ) )具体的$USER_NAME是否正确
    具体的$query1的sql语句是否正确
    具体的数据库链接是否正确
    具体的返回结果。是否正确
    知道了,便知道了问题所在
      

  6.   

    我测试过 打印不出user_name 的值  空值  估计哪里错了!
      

  7.   

    csv文件时逗号风格吧,看看里面什么内容?