2016-10-13 26 views
0

上傳CSV文件,我得到這個錯誤,而在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', 

這僅僅是第一row.I有大約500行這樣在我的csv文件

如果我打印查詢執行它顯示的數據完全

執行的查詢

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

嘗試的第一件事是,除去每行的末尾任何尾隨的逗號,因爲它的期待後的值。如果這不起作用,請確保您的字段類型與您的CSV條目匹配,即整數是數字,varchars是字符串等。只需使用帶有一行或兩行的文件進行測試即可。 – TimBrownlaw

+0

這將是多次使用'prepare()'和'execute()'的好時機 – RiggsFolly

+0

如果你回答查詢**並在你的問題中顯示它使用**,那將是非常有用的。 MYSQL不會讓這些錯誤發生,因爲它很無聊。 **很顯然,查詢不正確** – RiggsFolly

回答

0

因爲在mysql查詢中只有13個列名和20個以上的列數據值。

還要確保在插入單元格的值時使用``而不是''。

還要確保在插入單元格的值時使用mysqli_real_escape_string。

+0

我想刪除額外的,「從我的CSV文件,除了逗號分隔線 –

+0

使用PHP的trim()刪除''準備MySQL插入查詢..像implode(」','「,trim($ r ,「'」)) –

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