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

标签 php mysql

在 mysql 中上传 csv 文件时出现此错误

Column count doesn't match value count at row 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')

最佳答案

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

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

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

关于php - 在 mysql 中上传 CSV 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018109/

相关文章:

php - Backbone JS + Codeigniter Restful API 和 MYSQL 子字符串功能不起作用

php - 单击 php 中的链接(来自数据库)后没有任何显示

php - 使用$_GET向数据库插入数据注意事项?

mysql - 仅一条 SQL 中的两步 SQL 语句

PHP MySQL 双 While 循环给我带来问题

mysql - 复杂的MySQL查询错误结果

php - Laravel createConnection 超时

php - 如何使用 php 在 excel 表上重复输出

php - 重复数据库中的数据?

mysql - 两个不同的 View 具有不同的数据库排序顺序(default_scope)