使用我的这段代码它也是excel的导入标题我想在我在数据库中插入记录时跳过标题。这是我的csv上传文件我想在导入中跳过列的标题如何 我知道这有可能......
<?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 into 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>
答案 0 :(得分:0)
如果第一列是包含标题的列(主要是它),那么为什么不使用布尔值来表示它。例如: -
$isTitleSkipped = false;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
if(!$isTitleSkipped)
{
$isTitleSkipped = true;
continue;
}
$sql = "INSERT into 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);
}
警告: - 停止使用 mysql _ * 。 不推荐使用。
答案 1 :(得分:0)
我以这种方式得到了我的结果
$k= 1;
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
if($k!=1)
{
$sql = "INSERT into 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);
}
$k++;
}