Click here to Skip to main content
12,882,820 members (39,184 online)
Rate this:
Please Sign up or sign in to vote.
See more: PHP MySQL
I written script for the validation for the fields of csv file.I have done it successfully but if the conditions are false csv file is not importing in database.
Here is my code:
   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 />";
    $file_name = $_FILES["file"]["name"];
if($_FILES["file"]["type"] != "text/csv")
die("This is not a CSV file.");
move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);

/* From Validation */
  $handle = fopen($location . $_FILES["file"]["name"], 'r');
  if ($handle)
    //the top line is the field names`
    $fields = fgetcsv($handle, 4096, ',');
    //loop through one row at a time
       while (($data = fgetcsv($handle, 1000, '|')) !== FALSE)
      echo"there is an empty field for srno<br/>";
    echo"Invalid Date Format in Row "."$counter"."<br/>";
    echo"there is an empty field for date<br/>";
    if( !preg_match("/^[0-9]{10}/",$mobno) )
    echo "Please enter a valid phone number<br/>";
    echo"there is an empty field for mobile no<br/>";
if(!preg_match("/^[a-zA-Z0-9]+$/",$city,$state) == 1)
    echo"check your fields for state or city <br/>";
    echo"there is an empty field for City<br/>";
    echo"there is an empty field for State<br/>";
    echo"there is an empty field for Type<br/>";

    $query="SELECT telecaller FROM import2 WHERE telecaller='$telecaller'";
    echo"Check list for telecallers";
echo"All Sr.NO are only digital not contain any other number<br/>";
    echo"there is an empty field for telecaller<br/>";
$q="LOAD DATA INFILE '$file_name' IGNORE INTO TABLE import
                FIELDS TERMINATED BY '|'
                LINES TERMINATED BY '\n'
                IGNORE 1 LINES
                SET srno=srno,date=@date,mobno=@mobno,city=@city,
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 31-Jul-12 22:27pm
Updated 31-Jul-12 22:28pm
How do we know what is in your upload form (or Ajax code) on the client side? How do we know that the indices 'load' and 'file' are correct? How is this related to CSV or MySQL, if this is the POST problem? Why asking about something you don't show in code?
ryanb31 6-Aug-12 14:34pm
Your question is not clear.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170422.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100