简体   繁体   English

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

[英]Issue while uploading CSV file in mysql

I get this error while uploading csv file in mysql 在mysql中上传csv文件时收到此错误

Column count doesn't match value count at row 1 列数与第1行的值数不匹配

My Query is: 我的查询是:

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";
}

This is what my csv file looks like 这就是我的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',

This is only the first row.I have around 500 rows like this in my csv file 这只是第一行,我的csv文件中大约有500行

If i print the query before execution it shows data perfectly 如果我在执行前打印查询,它将完美显示数据

Executed Query 执行查询

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')

Its because you have only 13 column names and more than 20 column data values in the mysql query. 这是因为mysql查询中只有13个列名和20多个列数据值。

Also make sure you use `` instead of '' while inserting cell value in mysql. 还要确保在mysql中插入单元格值时使用``而不是''。

Also make sure you use mysqli_real_escape_string while inserting cell value in mysql. 还要确保在mysql中插入单元格值时使用mysqli_real_escape_string。

Check this Example hope it helps: 检查此示例,希望对您有所帮助:

//  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   //

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM