dongyukang7006 2016-05-14 21:46
浏览 33

PHP / MySQL插入错误的列

Wrote my code to work with a specific .cvs format and it was working for the format of the files I had. Now the format of the .cvs file has changed.

So now it looks like everything is inserting but it has shifted over. More specifically everything has moved over one whole column to the right.

I was able to modify the code to make it move further over to the right, but I was unsuccessful at moving it to the left.

Here the part of my code that is having issues:

if($deleteSuccess == 1){

    echo "Now inserting all new records";
    $handle = fopen($target_file, "r");
    $count = 0;
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
      $name = $data[0];
      $correct0 = str_replace("'", "", $name);
      $correct1 = str_replace("'", "", $data[1]);
      $correct2 = str_replace("'", "", $data[2]);
      $correct3 = str_replace("'", "", $data[3]);
      $correct4 = str_replace("'", "", $data[4]);
      $correct5 = str_replace("'", "", $data[5]);
      $correct6 = str_replace("'", "", $data[6]);
      $correct7 = str_replace("'", "", $data[7]);


      $import="INSERT into DVDCatalog.testtable(`Volume Name`,`DVD Number`,`Type`,`Category`,`Date`,`Author`,`Availablity`,`Picture`) values('$name','$correct1','$correct2','$correct3','$correct4','$correct5','$correct6','$correct7')";
      $count = $count + 1;
      mysqli_query($conn, $import);
      if(mysqli_error($conn)){
          echo "<br> Problem with --> ".$import."<br>";

Thanks.

  • 写回答

1条回答 默认 最新

  • dongzhi8984 2016-05-14 22:37
    关注

    If everything has moved over 1 column to the right, then the data that used to be in $data[0], $data[1], etc. is now in $data[1], $data[2], etc. To get insert data the same as you used to, you should not be doing anything with $data[0], because that's the new field that was added to the CSV file but doesn't correspond to the old format.

    So it should be:

      $correct0 = str_replace("'", "", $data[1]);
      $correct1 = str_replace("'", "", $data[2]);
      $correct2 = str_replace("'", "", $data[3]);
      $correct3 = str_replace("'", "", $data[4]);
      $correct4 = str_replace("'", "", $data[5]);
      $correct5 = str_replace("'", "", $data[6]);
      $correct6 = str_replace("'", "", $data[7]);
      $correct7 = str_replace("'", "", $data[8]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题