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

使用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';
                }
            }
    
    打赏 评论
  • dougan7657 2014-08-23 06:22

    You can put duplicate emails to an array when triggering else section of $strStatus == 0 condition :

                $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);
    
    打赏 评论

相关推荐 更多相似问题