1

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Use a foreach loop - that will then go through each new key and then you can query / do what ever you want with it.
You asked "how can i get the last inserted ids of all imported rows" - what do you need to do with the IDs of the inserted rows? I believe you would use the foreach loop when you need to do the update.
Yes so with the array you have you would do a foreach - php.net/manual/en/control-structures.foreach.php will give you more information.
1

Save the last id after a query:

// 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;

Info: http://www.w3schools.com/php/php_mysql_insert_lastid.asp

2 Comments

How can i get all last inserted ids?? I have tried your idea but getting last id only
The array declaration should be before the do instruction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.