php - skip title import csv file -
with code import title of excel want skip title while inserting records in database. csv upload file want skip title of columns in import how it's possible know it's possible..
<?php include 'header.php'; include 'footer.php'; if(isset($_post["import"])) { $db= 'ashutosh'; // database name. mysql_select_db($db) or die (mysql_error()); $filename=$_files["file"]["tmp_name"]; if($_files["file"]["size"] > 0) { $file = fopen($filename, "r"); while (($emapdata = fgetcsv($file, 10000, ",")) !== false) { $sql = "insert contact(user_id,contact_1,contact_2,contact_3,email,tin_no,cst_no) values ('$emapdata[1]','$emapdata[2]','$emapdata[3]','$emapdata[4]','$emapdata[5]','$emapdata[6]','$emapdata[7]')"; mysql_query($sql); } fclose($file); echo "<script>"; echo "swal('sucess!', 'file imported sucessfully!','success')"; echo "</script>"; } else { echo "<script>"; echo "sweetalert('oops','select csv file','error')"; echo "</script>"; } } ?> <div class="wrapper row-offcanvas row-offcanvas-left"> <?php include 'admin_leftside.php'; ?> <aside class="right-side"> <section class="content-header"> <h1>upload csv</h1> <ol class="breadcrumb"> <li><a href="#"><i class="fa fa-dashboard"></i> home</a></li> <li class="active">upload csv</li> </ol> </section> <form enctype="multipart/form-data" method="post" role="form"> <section class="content"> <div class="row"> <div class="col-md-6"> <div class="box box-primary"> <div class="box-header"></div> <div class="box-body"> <form method="post"> <div class="form-group"> <label for="exampleinputfile">file upload</label> <input type="file" name="file" id="file" size="150"> <p class="help-block">only csv file import.</p> </div> <div class="box-footer"> <button type="submit" class="btn btn-primary" id="submit" name="import" >submit</button> </div> </div> </div> </div> </div> </form>
if first column 1 containing title (mostly is) why not use boolean value indicate it. eg:-
$istitleskipped = false; while (($emapdata = fgetcsv($file, 10000, ",")) !== false) { if(!$istitleskipped) { $istitleskipped = true; continue; } $sql = "insert contact(user_id,contact_1,contact_2,contact_3,email,tin_no,cst_no) values ('$emapdata[1]','$emapdata[2]','$emapdata[3]','$emapdata[4]','$emapdata[5]','$emapdata[6]','$emapdata[7]')"; mysql_query($sql); }
warning:- stop using mysql_*. it deprecated.
Comments
Post a Comment