2016-05-24 17 views
1

インポートされたすべての行の最後に挿入されたIDを取得するにはどうすればよいですか?そして、date_participationの月と年の比較を使用して列を更新し、q1、q2、 q3、q4。 PHPコード:最後に挿入されたcsvのIDを取得する方法

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

         } 

         ?> 

答えて

1

ここソリューションはwhileループの外に配列を設定した後にmysql_insert_id()を使用して各挿入後に追加することです。そうすれば、追加されたすべての新しい行のセットが作成されます。

+0

どのように私が実装し、IDが配列に格納されている取得することができます.... –

+0

foreachループを使用して - その意志をそれぞれの新しい鍵を通過すると、あなたはそれであなたが望むものを照会/実行できます。 – Ukuser32

+0

ここで私は各関数のためにを使用します。 –

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

+0

最後に挿入されたIDをすべて取得する方法はありますか?私はあなたのアイデアを試みたが、最後のIDだけを取得している –

+0

配列宣言はdo命令の前になければならない。 –

+0

しかし最後に挿入されたid.if u rはthat..i implement .. –

関連する問題