在mysql中上传CSV文件时出现问题

时间:2016-10-13 10:10:07

标签: php mysql

我在mysql上传csv文件时遇到此错误

  

列数与第1行的值计数不匹配

我的查询是:

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

$file=$_FILES['file']['tmp_name'];
$handle=fopen($file,"r");

while(($r=fgetcsv($handle,"500",",")) !==FALSE)
{

    $sql="insert into `couponss` (promoid,offerid,offername,type,code,title,description,category,offerpage,dateadded,expiry,exclusive,featured) values('" . implode("','",$r)."')";

    $result=mysqli_query($con,$sql) or die(mysqli_error($con));

    //echo implode("','",$r);
}
}
else
{
echo "NO DIRECT SCRIPT ALLOWED";
}

这是我的csv文件的样子

P43183','1126','Giftease.com CPS - India','Coupon','GIFTICICI20','Get 20% Off on orders above INR 1200*','TnC: The offers are valid on all eligible orders placed between 1st April 2015 to 31st March 2017 The offers are available on all items ordered on www.giftease.com excluding gift cards/vouchers, select watches & gift wrapping charges. Offer limited to usage of the code to 3 orders per customer Eligible orders: the order value of a single order must be equal or higher than Rs. 1200 or Rs.4000 excluding any items belonging to the excluded categories mentioned above Coupon code \"GIFTICICI20\"" or \""GEICICI1000\"" must be applied in the shopping cart',' prior to payment for eligible orders',' in orders to avail the offer Eligible orders must be paid for using ICICI Bank credit card',' debit card or netbanking only. The offers cannot be combined with any other offer available on www.giftease.com from time to time. All order are subject to the terms of use of www.giftease.com published at http://www.giftease.com/terms-of-use. This offer may be withdrawn or modified at any time at the discretion of Giftease or ICICI Bank."','Gifts & Flowers','http://tracking.vcommission.com/aff_c?offer_id=1126&aff_id=51664&url=http%3A%2F%2Fwww.giftease.com%2F%3Futm_source%3Dvcommission%26utm_medium%3Dbanner_emailer%26utm_campaign','Sep 16, 2016','Mar 31, 2017','0','0',

这只是第一行。我的csv文件中有大约500行这样的行

如果我在执行前打印查询,它会完美地显示数据

执行查询

insert into `couponss` (promoid,
                        offerid,
                        offername,
                        type,
                        code,
                        title,
                        description,
                        category,
                        offerpage,
                        dateadded,
                        expiry,
                        exclusive,
                        featured) 
            values('P43183',
                    '1126',
                    'Giftease.com CPS - India',
                    'Coupon',
                    'GIFTICICI20',
                    'Get 20% Off on orders above INR 1200*',
                    'TnC: The offers are valid on all eligible orders placed between 1st April 2015 to 31st March 2017 The offers are available on all items ordered on www.giftease.com excluding gift cards/vouchers, select watches & gift wrapping charges. Offer limited to usage of the code to 3 orders per customer Eligible orders: the order value of a single order must be equal or higher than Rs. 1200 or Rs.4000 excluding any items belonging to the excluded categories mentioned above Coupon code \"GIFTICICI20\"" or \""GEICICI1000\"" must be applied in the shopping cart',
                ' prior to payment for eligible orders',
                ' in orders to avail the offer Eligible orders must be paid for using ICICI Bank credit card',
                ' debit card or netbanking only. The offers cannot be combined with any other offer available on www.giftease.com from time to time. All order are subject to the terms of use of www.giftease.com published at http://www.giftease.com/terms-of-use. This offer may be withdrawn or modified at any time at the discretion of Giftease or ICICI Bank."',
                'Gifts & Flowers',
                'http://tracking.vcommission.com/aff_c?offer_id=1126&aff_id=51664&url=http%3A%2F%2Fwww.giftease.com%2F%3Futm_source%3Dvcommission%26utm_medium%3Dbanner_emailer%26utm_campaign',
                'Sep 16, 2016',
                'Mar 31, 2017',
                '0',
                '0')

2 个答案:

答案 0 :(得分:0)

因为你在mysql查询中只有13个列名和20多个列数据值。

还要确保使用``而不是''在mysql中插入单元格值。

还要确保在mysql中插入单元格值时使用mysqli_real_escape_string。

答案 1 :(得分:0)

检查此示例希望它有所帮助:

//  File read and storre start //
$tmpName = $_FILES['invitecollegescsv']['tmp_name'];
$instituteAarray = array_map('str_getcsv', file($tmpName));
foreach($instituteAarray as $k=>$v)
{
    if($k>0)
    {
    $InsertInvites = $conn->query("insert into r_job_invitations (id_job,email,inv_st,inv_res,inv_confirmation) values ('".$jobId."','".$v[0]."','".$inv_st."',0,'".$job_response."')");
    }
}
//   File read and store ends here   //