title: How do I validate CSV Contents before uploading and save it to msql database using php?
body: <p>I searched everywhere in the internet but I couldn't find a perfect solution for my question, regarding on how to validate the CSV Contents before it uploads and save it to database, I am using msqli for the database and PHP for the language script. For example I have 3 columns in localhost database:</p>
  
  <p>These are my following headers I made both in locahost database and in CSV File (1.)Date (2.)Email (3.)Phone Number.</p>
  
  <p>Before uploading and saving to the localhost database it should meet the restrictions of the contents are the ff:
  For the Date: It should mm/dd/yy ----> 1/31/2018 or 12/31/2018
  For the Email: It should name@domain.com ----> saffron@gmail.com
  For the Number: It should 12 digits number only and the format is ----> 0906-021-0156</p>
  
  <p>If those restrictions meets perfectly the CSV File will be uploaded and save to the database, if not it will throw an error or pop-up message.</p>
  
  <p>I really don't know how to start the execution of program. I am really new to PHP so please help me with this.</p>
  
  <p>This is the code I worked, and I am stack here...</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
  $dbHost = 'localhost';
  $dbUsername = '';
  $dbPassword = 'root';
  $dbName = 'dbUpload';
  
  $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  
  if ($db-&gt;connect_error) {
      die("Unable to connect database: " . $db-&gt;connect_error);
  }
  
  if(isset($_POST['submit'])){
   
   $row = 1;
   $mycsvfile = array(); //define the main array.
   
   if ($_FILES['csv']['size'] &gt; 0) {
       //get the csv file 
        $file = $_FILES['csv']['tmp_name']; 
        $handle = fopen($file, "r");
        $i = 0;
        
          while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  
          $num = count($data); 
           $row++;
   
        $mycsvfile[] = $data; //add the row to the main array.
        
        $headerDate = $mycsvfile[0][0];
        $headerEmail = $mycsvfile[0][1];
        $headerPhone =  $mycsvfile[0][2];
  
        if ($headerDate !== 'Date' || $headerEmail !== 'Email' || $headerPhone !== 'Phone Number') {
          $qstring = '?status=invalid_header';        
          fclose($handle);     
         }
         
         else {
          if ($i &gt; 0) {
             
                $import = "INSERT into upload (techDate, techEmail, techPhone)values('$data[0]','$data[1]','$data[2]')";
                
                $db-&gt;query($import);
                $qstring = '?status=succ';
            }
         }
        $i++;        
    } 
    fclose($handle);
    
    
  
  }
  else{
   $qstring = '?status=err';
     }
  }
  header("Location: uploadvalid.php".$qstring);
  ?&gt;</code></pre>
  </div>
  </div>
  </p>