0
<?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>

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 [email protected]  trainee 6000\t
Syed    101 [email protected]  trainee 6000\t
Raja    102 [email protected]  trainee 6000\t
Murali  103 [email protected]    trainee 6000\t
Giri    104 [email protected]  trainee 6000\t
Sekar   105 [email protected] trainee 6000\t
1
  • The best way to check for duplicate records is with a SQL statement. I'm not seeing any SQL in you code. For example: Find duplicate records in mySQL Commented Aug 23, 2014 at 6:15

2 Answers 2

1

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("\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';
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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);

1 Comment

i put this code, but it prints the last mail id in the record only sir. i gave two mail ids same, but it prints the second mail id only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.