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

时间:2016-05-24 07:43:37

标签: php csv m

如何获取所有导入行的最后插入ID。并使用date_participation比较月份和年份更新列,然后将其插入当前的quter列,如q1,q2,q3,q4。             php代码:

             <?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 个答案:

答案 0 :(得分:1)

这里的解决方案是在while循环之外设置一个数组,然后在每次插入后使用mysql_insert_id()添加它。这样,您将拥有一组已添加的所有新行。

答案 1 :(得分:1)

在查询后保存最后一个ID:

// Before the loop
$list_ids = [];

[...]
$q = 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"))."'                     
            )  
        "); 


$list_ids[] = $q->insert_id;

信息http://www.w3schools.com/php/php_mysql_insert_lastid.asp