php - 使用导入上传 CSV 文件时,仅导入 1 条记录

标签 php mysql csv

我们的 CSV 文件中有数千条记录,我想一次性上传。当我选择 CSV 文件时,只有 1 条记录导入到 mysql 表中。请检查代码并让我知道如何解决它。

    <?php
//load the database configuration file
include 'csv_css.css';
include 'dbConfig.php';

if(!empty($_GET['status'])){
switch($_GET['status']){
    case 'succ':
        $statusMsgClass = 'alert-success';
        $statusMsg = 'Members data has been inserted successfully.';
        break;
    case 'err':
        $statusMsgClass = 'alert-danger';
        $statusMsg = 'Some problem occurred, please try again.';
        break;
    case 'invalid_file':
        $statusMsgClass = 'alert-danger';
        $statusMsg = 'Please upload a valid CSV file.'; 
        break;
    default:
        $statusMsgClass = '';
        $statusMsg = '';
}
}
?>
<div class="container">
<?php if(!empty($statusMsg)){
    echo '<div class="alert '.$statusMsgClass.'">'.$statusMsg.'</div>';
} ?>
<div class="panel panel-default">
    <div class="panel-heading">
        Members list
        <a href="javascript:void(0);" onclick="$('#importFrm').slideToggle();">Import Members</a>
    </div>
    <div class="panel-body">
        <form action="importdata.php" method="post" enctype="multipart/form-data" id="importFrm">
            <input type="file" name="file" />
            <input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
        </form>
        <table class="table table-bordered">
            <thead>
                <tr>
                <th>id</th>
               <th>ContactID</th>
            <th>ContactOwner</th>
            <th>LeadSource</th>
            <th>First_name</th>
            <th>Last_name</th>
            <th>AccountName</th>
            <th>Title</th>
            <th>EmailID</th>
            <th>Department</th>
            <th>Industry</th>
            <th>Phone</th>
            <th>Mobile</th>
            <th>Today_date</th>
            <th>LinkedIn</th>
            <th>CallStatus</th>
            <th>Website<th>
            <th>Street</th>
            <th>OtherStreet</th>
            <th>City</th>
            <th>State</th>
            <th>Zip</th>
            <th>Country</th>
            <th>Description</th>
                </tr>
            </thead>
            <tbody>
            <?php
                //get records from database
                $query = $db->query("SELECT * FROM contact ORDER BY ContactID DESC");
                if($query->num_rows > 0){ 
                    while($row = $query->fetch_assoc()){ ?>
                <tr>
                   <td><?php echo $row['id'];?></td>
                  <td><?php echo $row['ContactID']; ?></td>
                  <td><?php echo $row['ContactOwner']; ?></td>
                  <td><?php echo $row['LeadSource']; ?></td>
                  <td><?php echo $row['First_name']; ?></td>
                  <td><?php echo $row['Last_name']; ?></td>
                  <td><?php echo $row['AccountName']; ?></td>
                  <td><?php echo $row['Title']; ?></td>
                  <td><?php echo $row['EmailID'];?></td>
                  <td><?php echo $row['Industry']; ?></td>
                  <td><?php echo $row['Department']; ?></td>
                  <td><?php echo $row['Phone']; ?></td>
                  <td><?php echo $row['Mobile']; ?></td>
                  <td><?php echo $row['Today_date']; ?></td>
                  <td><?php echo $row['LinkedIn']; ?></td>
                  <td><?php echo $row['CallStatus']; ?></td>
                  <td><?php echo $row['Website'];?></td>
                  <td><?php echo $row['Street']; ?></td>
                  <td><?php echo $row['OtherStreet']; ?></td>
                  <td><?php echo $row['City']; ?></td>
                  <td><?php echo $row['State']; ?></td>
                  <td><?php echo $row['Zip']; ?></td>
                  <td><?php echo $row['Country']; ?></td>
                  <td><?php echo $row['Description']; ?></td>
                </tr>
                <?php } }else{ ?>
                <tr><td colspan="24">No member(s) found.....</td></tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
</div>

导入数据.php

    <?php
//load the database configuration file
include 'dbConfig.php';

if(isset($_POST['importSubmit'])){

//validate whether uploaded file is a csv file
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
    if(is_uploaded_file($_FILES['file']['tmp_name'])){

        //open uploaded csv file with read only mode
        $csvFile = fopen($_FILES['file']['tmp_name'], 'r');

        //skip first line
        fgetcsv($csvFile);

        //parse data from csv file line by line
        while(($line = fgetcsv($csvFile)) !== FALSE){
            //check whether member already exists in database with same email
            //insert member data into database
$db->query("INSERT INTO contact (id, ContactID, ContactOwner, LeadSource, First_name, Last_name, AccountName, Title, EmailID, Industry, Department, Phone, Mobile, Today_date, LinkedIn, CallStatus, Website, Street, OtherStreet, City, State, Zip, Country, Description) VALUES ('".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."','".$line[5]."','".$line[6]."','".$line[7]."','".$line[8]."','".$line[9]."','".$line[10]."','".$line[11]."','".$line[12]."','".$line[13]."','".$line[14]."','".$line[15]."','".$line[16]."','".$line[17]."','".$line[18]."','".$line[19]."','".$line[20]."','".$line[21]."','".$line[22]."','".$line[23]."')");
        }

        //close opened csv file
        fclose($csvFile);

        $qstring = '?status=succ';
    }else{
        $qstring = '?status=err';
    }
}else{
    $qstring = '?status=invalid_file';
}
}

//redirect to the listing page
header("Location: usait.php".$qstring);

最佳答案

这通常是由于您没有在 ID 列上设置“自动增量”而引起的,请确保您为 MySQL 表上的 ID 列启用了自动增量,并且脚本应该按预期工作。

关于php - 使用导入上传 CSV 文件时,仅导入 1 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590711/

相关文章:

php - jQuery/AJAX 不使用 Varchar ID 更新记录

php - 产品有货时显示自定义产品徽章 WooCommerce

PHP mySQL - 如何在使用 IN 后从行获取数据

php - 在 PHP 中创建 DAO 的正确方法

python - 从 Python 中的 url 读取 gzip csv 时出错 : "_csv.Error: line contains NULL byte"

php - 用php删除html元素

mysql - 从员工表中获取偶数记录

mysql - MySQL 中的 UPDATE ... WHERE 事务是否会锁定从子查询引用的其他表中的行?

java - 使用 XSL 将 XML 动态转换为 CSV - 无法获取标签名称

java - Spring 批处理 : reading a csv file and modify this list before putting the results in another file