Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
PHP
 function connect_db()

            {

                    $con = mysql_connect("localhost","root","");
 
                                 if (!$con)
 
                                    {
            
                                            die('Could not connect: ' . mysql_error());
 
                                    }
                     mysql_select_db("import", $con);
            }
  
 
    if (isset($_POST['load']))
        { 
            if ( isset($_FILES["file"]))
                 {
                    //if there was an error uploading the file
                    if ($_FILES["file"]["error"] > 0) 
                            {
                                echo "File Not Selected  <br />";
                            }
                                   
                                     else 
                                    {
                                         //Print file details
                                            
                                            // echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                                             
                                            // echo "Type: " . $_FILES["file"]["type"] . "<br />";
                                             
                                            // echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                                            
                                            // echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

                                         //if file already exists
                                                 $file_name = $_FILES["file"]["name"];
                                             if (file_exists("$file_name"))
             
                                                     {
               
                                                          echo $_FILES["file"]["name"] . " already exists. ";
          
                                                       }
             
                                           elseif($_FILES["file"]["type"] != "application/vnd.ms-excel")
                                                        { 
           	
                                                                die("This is not a CSV file.");
            
                                                        }
                                                            else 
         
                                                                    {
                                                                        //Store file in directory "upload" with the name of "uploaded_file.txt"
                                                     
                                                                    
                                                                     $location="C:\\xampp\\mysql\\data\\import\\";
          
                                                        move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);
       
                
                connect_db();        
                 
         $q="LOAD DATA INFILE '$file_name' INTO TABLE import
                FIELDS TERMINATED BY '|'
                LINES TERMINATED BY '\n'
                IGNORE 1 LINES 
                (srno,@date,@mobno,@state,@type,@telecaller)
                SET srno=srno,date=@date,mobno=@mobno,
                state=@state,type=@type,telecaller=@telecaller";
                       
                                                                 
                                                                   
mysql_query($q) or die(mysql_error());
                              
     echo"File Uploaded and Updated Successfully";
                             

mysql_query("INSERT INTO temp1 SELECT mobno,telecaller FROM import GROUP BY mobno HAVING COUNT(*)>1 ") or die(mysql_error());

mysql_query("UPDATE import,temp1 SET import.telecaller=temp1.telecaller WHERE import.mobno=temp1.mobno");
                  } 
                    }
             }          
            }

?>
Posted
Updated 20-Jul-12 0:24am
v2
Comments
[no name] 20-Jul-12 7:41am    
This is a code dump and not any sort of a question.
Sebastian T Xavier 20-Jul-12 12:57pm    
Is there any problem?
Kislay Raj 8-Aug-12 15:07pm    
what error message are you getting?

1 solution

try
SQL
LOAD DATA INFILE '$file_name' INTO TABLE import
Ffields terminated by ','
enclosed by '"'
lines terminated by '\n'
IGNORE 1 LINES 
                (srno,date,mobno,state,type,telecaller)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900