CSV File Upload Import Database In php mysql

CSV File Upload Import Database In php mysql
http://www.developerinvention.in/2016/04/how-to-create-and-download-csv-file.html?m=1 More data csv format insert database import csv format data.
Create can file

CSV File Upload Import Database In php mysql

CSV File Upload Import Database In php mysql


<?php
include("includes/header.php");
function remostrtag($a)
{
    return mysql_escape_string(trim($a));
}
 $error_msg='';
function redvv($b)
{
    return str_replace(" ","",strtolower($b));
}
if (isset($_REQUEST['submit']) || isset($_REQUEST['submit_x'])) {
    
     $file_size = $_FILES['cfile']['size']; 
  // echo  "===>>>".$file_type = $_FILES['cfile']['type']; exit;
    
   
   $filecheck = basename($_FILES['cfile']['name']);
 $ext = substr($filecheck, strrpos($filecheck, '.') + 1);
   /*
 if (($file_size > 2097152)){      
        $message = 'File too large. File must be less than 2 megabytes.'; 
        echo '<script type="text/javascript">alert("'.$message.'");</script>'; 
    }
 */
 $repary=array();
  if(strtolower($ext)=='csv')
   {
$handle = fopen($_FILES['cfile']['tmp_name'], "r");
$hj=0;
while (($data = fgetcsv($handle, 4000, ",")) !== FALSE) {
//print_r($data).'<br/><br/>';
            $hj++;
            $sts=0;
              if($hj==1 && (redvv($data[0])!='sl.no' || redvv($data[1])!='name' || redvv($data[2])!='sex'  || redvv($data[3])!='yearofstudy' || redvv($data[4])!='class'   || redvv($data[5])!='presentposition'  || redvv($data[6])!='telephoneno' || redvv($data[7])!='emailid')){
                header("location:alumnicsvfile.php?fort");
   exit; 
            }
          
            
            if($hj>1){
                $qrychk=mysql_query("select * from tblmini where LOWER(`firstname`)='".strtolower(remostrtag($data[1]))."'");
               // if(mysql_num_rows($qrychk) =0)
                //{
//                   $grpid=getret_cln_grpid($data[3]); 
                   // if(strtolower($data[11])=='active')
                 //   {
                        $sts=1;
                 //   }
                    
                    $add=$data[5].','.$data[6];
$emapass = explode('@',$data[7]);
$passd = $emapass[0].'123';
// ECHO $passd.'<br/><br/>';
         $import="INSERT into tblmini(firstname,gender,batch,phone,email,password) values('$data[1]','$data[2]','$data[3]','$data[6]','$data[7]','$passd')";
//echo $import; 
mysql_query($import);
                //}else{
                //    $repary[]=$data[0];
               // }
}
        }
       //exit;
fclose($handle);
         if(!empty($repary))
        {
             $error_msg='S.no '.implode(",",$repary).' values are not inserted';
        }else{
   header("location:mnicsvfile.php?suc");
   exit;
        }
}else
{
    header("location:mnicsvfile.php?err");
   exit;
    $error_msg='Please upload Correct CSV file';
}
}
     ?>
 
<div id="content" class="app-content" role="main">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3" style="font-size: 22px;">
            Upload <i class="fa fa-angle-double-right"></i> Alumni CSV File Upload
        </h1>
    </div>
    <div class="wrapper-md">
        <!-- toaster directive -->
        <toaster-container
            toaster-options="{'position-class': 'toast-top-right', 'close-button':true}"></toaster-container>
        <!-- / toaster directive -->
        <div class="panel panel-default">
            <div class="panel-heading" style="height: 3.5em">
                <span style="color:#F00;float:left;font-style: none">* Marked fields are mandatory</span>
              
            </div>
            <div class="panel-body">
                <form  method="post" action="" class="form-validation" enctype="multipart/form-data">
                    <div class="form-group">
<?php
if( $error_msg!=''){
     echo '<div class="col-sm-12 fromdel" >'.$error_msg.'</div>';
}else{
if (isset($_REQUEST['suc'])) {
    echo '<div class="col-sm-12 fromsuc" style="float:left; height:30px;">Successfully Imported</div>';
}
if (isset($_REQUEST['up'])) {
    echo '<div class="col-sm-12 fromupd" >Successfully Updated</div>';
} if (isset($_REQUEST['del'])) {
    echo '<div class="col-sm-12 fromdel" >Successfully Deleted</div>';
}
if (isset($_REQUEST['err'])) {
    echo '<div class="col-sm-12 fromdel" style="color:red">Please upload Correct CSV file</div>';
}
if (isset($_REQUEST['fort'])) {
    echo '<div class="col-sm-12 fromdel" style="color:red">Please upload Correct field name in CSV file</div>';
}
}
?>
                    </div>
                  
                    <div class="line line-dashed b-b line-lg pull-in"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Csv File<span style="color:#F00">*</span></label>
                        <div class="col-sm-10">
                            <div class="btn-group">
                                <input type="file" name="cfile" />
                            </div>
                        </div>
                    </div>
                    <div class="line line-dashed b-b line-lg pull-in"></div>
                   
                    <footer class="panel-footer text-center bg-light lter">
 <button type="submit"
                                    class="btn m-b-xs btn-sm btn-success btn-addon"
                                    name="submit" value="submit" >
                                <i class="fa fa-save"></i>Upload
                            </button>
                    </footer>
                </form>
            </div>
        </div>
    </div>
</div>  
<?php
include("includes/footer.php");
?>

CSV File Upload Import Database In php mysql Dev2Tricks 5 of 5
CSV File Upload Import Database In php mysql http://www.developerinvention.in/2016/04/how-to-create-and-download-csv-file.html?m=1 More d...

Share this

Related Posts

Previous
Next Post »