dongzhan2461 2016-05-24 07:43
浏览 27

如何在php中获取最后插入的csv ids

how can i get the last inserted ids of all imported rows.And update the columns using date_participation comparison of month and year then it insert into the current quter columns like q1,q2,q3,q4. php code:

             <?php
             include("includes/header.php"); 
             session_start();

             if(!isset($_SESSION['u_id']))
                      {
               //header("Location: membershippage.php");
                         }

              if ($_FILES[csv][size] > 0) { 

                    $file = $_FILES[csv][tmp_name]; 
                    $handle = fopen($file,"r");  
                      do { 
    if ($data[0]) { 
        mysql_query("INSERT INTO point_transfer (mbid,name,activity_name,date_participation,points, admin_id, uploaded_date) VALUES 
            ( 
                '".addslashes($data[0])."', 
                '".addslashes($data[1])."', 
                '".addslashes($data[2])."',
                 '".addslashes($data[3])."',
                 '".addslashes($data[4])."',
                 '".addslashes($_SESSION['u_id'])."',
                 '".addslashes(date("m/d/Y"))."'                     
            )  
        "); 
    }

} while ($data = fgetcsv($handle,1000,",","'")); 
                        //$last_id = mysql_insert_id();
                        $sql=("SELECT * FROM point_transfer where id = '".mysql_insert_id()."' ");
                        $result=mysql_query($sql);
                        while($row=mysql_fetch_array($result))

                        {
                        $mbid =  $row['mbid'];
                            echo $mbid;

                        $points = $row['points'];

                        $mons = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
                      $date_participation = $row['date_participation'];
                     list($month, $day, $year) = split('[/.-]', $date_participation);
                     }  
            $sql=("SELECT * FROM total_points WHERE mbid='".$mbid."'");
             $query=mysql_query($sql);
            $row=mysql_fetch_array($query);
            $month=$mons[$month];

            $q1 = array("January","February", "March");
            $q2 = array("April","May", "June");
            $q3 = array("July","August", "September");
            $q4 = array("October","November", "December"); 

        if (in_array($month, $q1))
         {
     $add=$row['q1'];

             }
            elseif(in_array($month, $q2))
                 {

             $add=$row['q2'];

             }
            elseif(in_array($month, $q3))
                 {
            $add=$row['q3'];
                }
                 elseif(in_array($month, $q4))
                   {
                     $add=$row['q4'];
                          }

                   $addition=$points + $add;

                       if (in_array($month, $q1))
                           {


                             $query=("update total_points SET q1='".$addition."' where mbid='".$mbid."'");
    $result=mysql_query($query , $con);



                           }
                     elseif(in_array($month, $q2))
                                {

    $query=("update total_points SET q2='".$addition."' where mbid= '".$mbid."' ");
    $result=mysql_query($query , $con);



                       }
              elseif(in_array($month, $q3))
                      {

    $query=("update total_points SET q3='".$addition."' where mbid='".$mbid."'");
    $result=mysql_query($query , $con);



                  }
              elseif(in_array($month, $q4))
                {

    $query=("update total_points SET q4='".$addition."' where mbid='".$mbid."' ");
                  $result=mysql_query($query , $con);
                       }

            echo ("<SCRIPT LANGUAGE='JavaScript'>

          window.location.href='point_transfer.php';
                </SCRIPT>");

                  die; 

                       } 

                        ?>
  • 写回答

2条回答 默认 最新

  • dream752614590 2016-05-24 08:00
    关注

    Your solution here is to set an array outside the while loop and then add to it after each insert using mysql_insert_id(). That way you will have a set of all the new rows that have been added.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行