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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器