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

时间:2014-08-23 06:10:15

标签: php 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("\r\n", "\r", "\n"), "", $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>

这是我将文本文件导入mysql数据库的代码。我检查记录中的邮件ID重复。现在我想在我的设计中显示重复的记录。请帮我。我的文本文件是:

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 个答案:

答案 0 :(得分:1)

我希望它能解决你所期望的...... :)

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("\r\n", "\r", "\n"), "", $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]. "\n";
                            }
                        }
                        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 :(得分:0)

当触发$ strStatus == 0条件的else部分时,您可以将重复的电子邮件放到数组中:

            $duplicateEmails = array();
            $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++;
                        $duplicateEmails[$strDuplicationRecords] = $strDatas[$k]['2'];
                    }
                }
           print_r ($duplicateEmails);