duaeim2874 2017-02-21 15:48
浏览 73
已采纳

使用PHP将CSV文件导入MySQL

I'm struggling with a problem.

So what i try is to import a csv file into a table.

Code:

-

But here's the catch: When import a file. It says; Success!, but nothing had imported.

What am I not seeing?

Thanks!

EDIT:

-

From the edited code I only getting the message: $satus=toegevoegd. Translated: Added to database. But there is no data in the table... I really dont know what im doing wrong.

EDIT 2;

-
  • 写回答

1条回答 默认 最新

  • douke3442 2017-02-23 12:58
    关注

    There's lots of good advice in the help centre - notably How to create a Minimal, Complete, and Verifiable example.

    As it stands there is no error checking in your code (which should be required for production use, never mind development). That the script ends with a redirect makes it rather hard to report detailed events for debugging (but these can be written to log files).

    ini_set('error_reporting', E_ALL);

    I am guessing this is the mysqli extension you are using here - if a query or connection fails, it does not trigger an eror in PHP, you need to poll mysqli_error() and/or check the return value from the operation.

    The SELECT/UPDATE/INSERT is very inefficient - a better approach would be to

    • use REPLACE if you always want to override the existing data,
    • OR, if you want to be a bit more selective
      • attempt the insert then handle duplicate key conflicts when they arise
      • or use a trigger to multiplex the insert/update behaviour.

    Depending on how the CSV is constructed you will get an error for the last line in the file - you might want to check 4<=count($line) before attempting to access the record.

    Your code is vulnerable to SQL Injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题