ds2010630 2014-08-23 06:10
浏览 404
已采纳

使用php将txt文件导入mysql数据库

<?php
    include("includes/config.php"); 
    if ($_POST['frmSubmit']) {
        $file = $_FILES['frmUpload']['tmp_name'];          //   Get Temporary filename
        if ($file) {
            $handle = fopen($file,"r");              // Open the file and read
            while ($strBookData = fgets($handle, 4096)) {
                $strData[] = $strBookData;
                $strDataCheck = str_replace(array("
", "", "
"), "", $strData);
                $strRowCheck = explode('\t', implode($strDataCheck));
            }
            foreach ($strRowCheck as $strRow){
                if (!empty($strRow)) {
                    $strRowValue = explode('    ', $strRow);
                    $strDatas[] = $strRowValue;
                    $strTableColumn = count($strRowValue);
                }
            }
            if ($strDatas) {
                $strInsertRecords = 0;
                $strDuplicationRecords = 0;
                if ($strTableColumn == 5) {
                    for ($k=1; $k<count($strDatas); $k++) { 
                        $strStatus = doCheckDuplication($strDatas[$k]['2']);
                        if ($strStatus == 0) {  
                            doInsertEmployeeDetails($strDatas[$k]['0'], $strDatas[$k]['1'], $strDatas[$k]['2'], $strDatas[$k]['3'], $strDatas[$k]['4']);
                            $strInsertRecords++;
                        } else {
                            $strDuplicationRecords++;
                        }
                    }
                    if (count($strDatas)-1 == $strInsertRecords) {
                        $strMsg = 'Employee details inserted successfully';
                        $strClass = 'Succes';
                    }
                    if (count($strDatas)-1 != $strInsertRecords) {
                        $strMsg = 'Employee details inserted successfully, some of names already exists';
                        $strClass = 'Error';
                    }
                    if (count($strDatas)-1 == $strDuplicationRecords) {
                        $strMsg = 'Employee details are already exists';
                        $strClass = 'Error';
                    }
                } else {
                    $strMsg = 'Column mis-match, Please verify the file.';
                    $strClass = 'Error';
                }
            }   
        } else {
            $strMsg = 'Please upload a valid file.';
            $strClass = 'Error';
        }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Employee Details</title>
<link href="css/employee.css" rel="stylesheet" type="text/css"/>
<script src="js/employee.js" type="text/javascript"></script>
</head>

<body>
<form id="frmEmployee" name="frmEmployee" enctype="multipart/form-data" method="post" action="" onsubmit="return validation();">
<div class="all">
  <div class="main">
    <div class="inner">
      <div class="top">
        <p>&nbsp;</p>
        <div class="text" align="center">
          <p class="det">EMPLOYEE DETAILS</p>
        </div>
        <p>&nbsp;</p>
      </div>
      <p>&nbsp;</p>
       <div align="center"><p class="<?php echo $strClass; ?>"><?php echo $strMsg; ?></p></div>
      <p>&nbsp;</p>
      <div class="nnn">
        <div class="name">Text  file Upload:</div>
        <div class="field">
            <label>
              <input type="file" name="frmUpload" id="frmUpload" onblur="checkEmpty('frmUpload', 'error_file', 'Please upload your file');"/>
            </label>
        </div>
        <p>&nbsp;</p>
      </div>
      <div class="span">
        <div class="span2"><span id="error_file"></span></div>
        <p>&nbsp;</p>
        </div>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <div class="submit">
        <div class="sub">
          <div class="but">
            <label>
            <input type="submit" name="frmSubmit" id="frmSubmit" value="Submit" class="subb" />
            </label>
          </div>
          <div class="but">
            <label>
            <input type="reset" name="frmReset" id="frmReset" value="Reset" class="subb" />
            </label>
          </div>
        </div>
        <p>&nbsp;</p>
        </div>
      </div>
    <p>&nbsp;</p>
    <?php  if ($_POST['frmSubmit']) { ?>
    <div class="info" id="one">
      <table width="64%" border="1" bordercolor="#DEDEDE" class="tabb">
        <tr>
          <td width="62%"><p class="rec">Total Records:</p> </td>
          <td width="38%"><p class="rec"><?php echo count($strDatas)-1; ?></p></td>
        </tr>
        <tr>
          <td><p class="rec">Inserted Records:</p></td>
          <td><p class="rec"><?php echo $strInsertRecords; ?></p></td>
        </tr>
        <tr>
          <td><p class="rec">Duplicate Records:</p></td>
          <td><p class="rec"><?php echo $strDuplicationRecords; ?></p></td>
        </tr>
      </table>
    </div>
      <?php } ?>
    </div>
  </div>
  </form>
</body>
</html>

this is my code for importing a text file into mysql database. i check duplication with mail id in the records. now i want to display the duplicate records in my design. please help me. and my text file is:

Name    Code    Mail    Designation Salary\t
Hari    100 hari@gmail.com  trainee 6000\t
Syed    101 syed@gmail.com  trainee 6000\t
Raja    102 raja@gmail.com  trainee 6000\t
Murali  103 murali@gmail.com    trainee 6000\t
Giri    104 giri@gmail.com  trainee 6000\t
Sekar   105 sekar@gmail.com trainee 6000\t
  • 写回答

2条回答 默认 最新

  • douhuan1937 2014-08-26 12:06
    关注

    I hope it will solve what you have expect for... :)

    if ($_POST['frmSubmit']) {
        $file = $_FILES['frmUpload']['tmp_name'];                     //    Get Temporary filename
        if ($file) {
            $handle = fopen($file,"r");                     //  Open the file and read
            while($strBookData = fgets($handle, 4096)) {
                $strData[] = $strBookData;
                $strData1 = str_replace(array("
    ", "", "
    "), "", $strData);
                $rows = explode('\t', implode($strData1));
            }
    
                foreach ($rows as $row){
                    if(!empty($row)){
                        $strDatasValue = explode('|', $row);
                        $strDatas[] = $strDatasValue;
                        $strTableColumn = count($strDatasValue);
                    }
                 }
    
               if ($strDatas) {
                    $strInsertRecords = 0;
                    $strDuplicationRecords = 0;
                    $strDuplicateData = array();
                    $strDuplicateMail = "";
                    if ($strTableColumn == 7) {
                        for($k=1; $k<count($strDatas); $k++) { 
                            $strStatus = doCheckDuplication($strDatas[$k]['2']);
                            if ($strStatus == 0) {  
                                $strData = $strDatas[$k];
                                    doInsertEmployeeDetails($strData['0'], $strData['1'], $strData['2'], $strData['3'], $strData['4'], $strData['5'], $strData['6']);
                                    $strInsertRecords++;
                                } else {
                                    $strDuplicationRecords++;
                                    $strDuplicateData[$strDuplicationRecords] = $strDatas[$k]['2'];
                                    $strDuplicateMail.= $strDuplicateData[$strDuplicationRecords]. "
    ";
                                }
                            }
                            if  (count($strDatas)-1 == $strInsertRecords) {
                                $strMessage = 'Employee record(s) inserted successfully!';
                                $strClass = 'Success';
                            }
                            if  (count($strDatas)-1 != $strInsertRecords) {
                                $strMessage = 'Employee record(s) inserted successfully but some of record(s) are already exists.!';
                                $strClass = 'Error';
                            }
                            if  (count($strDatas)-1 == $strDuplicationRecords) {
                                $strMessage = 'Employee record(s) are already exists.!';
                                $strClass = 'Error';
                            }
                        } else {
                            $strMessage = 'Column mis-match, Please verify the file.';
                            $strClass = 'Error';
                        }
                    }
                } else {
                    $strMessage = 'Please upload a valid file.';
                    $strClass = 'Error';
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波