2012-06-28 4 views
1

자동차가있는 테이블이 있습니다. 각 차량에는 하나 이상의 튜닝 단계가 다른 테이블에 있습니다.
튜닝 단계가있는 표에서; 자동차 식별자 : [cId] = 자동차 ID가 있습니다.테이블 1의 행 하나와 PHP 2를 사용하는 테이블 2의 여러 행을 복사하는 방법

먼저 자동차를 복제하여 mysql_insert_id()의 새 생성 ID를 $new_carId에 저장합니다.
그런 다음 자동차 식별자 [cId]를 기반으로 모든 관련 튜닝 단계를 가져오고 자동차와 동일하게 수행하지만 while() -loop에서 수행합니다.

이제 새로운 중복 단계에는 오래된 자동차의 [cId]가 있습니다.
이제 중복 된 단계가 새 차에 할당되도록 $new_carId에 저장된 ID로 [cId]를 대체해야합니다.

내가 보는 방식. 두 가지 옵션이 있습니다.
INSERT -query 또는
에 직접 입력하십시오. -loop 끝에 SQL UPDATE을 입력하십시오. 여기

지금까지 내 코드입니다 :

/** 
* Duplicate an existing car with associated tuning stage: 
* Each car may have more than one associated tuning stage located in a separate table. 
* We need to make sure all stages is duplicated as well, and assigned to the new car. 
* 
* [cId] car id. 
* 
* debug() is a personal debugging function. 
*/ 
    if (isset($_POST['duplicate_car'])){ 
    # duplicate the car. 
     $orgCar_id = (int)mysql_real_escape_string($_POST['orgCar_id']); // ID of the car we make the duplicate from. 
     $sql_car = 'INSERT INTO TUNE_cars (make, model, chassis, motor, motorkode, orgEff, orgNm, year, options, note) 
      SELECT make, model, chassis, motor, motorkode, orgEff, orgNm, year, options, note FROM TUNE_cars WHERE cId = '.$orgCar_id; 
     // debug($sql_car); 
     $qry_car = mysql_query($sql_car); 
     $new_carId = (int)mysql_real_escape_string(mysql_insert_id()); // We need to attach this ID to the new duplicated tuning stages. 
    // 
    /** 
    * Duplicate any associated tuning stages: 
    * We need to fetch all stages associated with the old car, 
    * duplicate them aswell, and attach them to the new car. 
    * 
    * [sId] stage id 
    * [cId] car id. This connects the stages to a given car. 
    */ 
     # fetch all stages associated with the old car. 
      $sql_stages = 'SELECT sId FROM TUNE_stages WHERE cId = '.$orgCar_id; 
      // debug($sql_stages); 
      $qry_stages = mysql_query($sql_stages); 
     // 
     # duplicate the stages. 
      while($get_stage = mysql_fetch_assoc($qry_stages)){ 
       $sql_dup_stage = 'INSERT INTO TUNE_stages (cId, stage, stageHp, stageNm, stagePrice, stageMethod, stageDyno, sinfo, snote) 
       SELECT '.$new_carId.', stage, stageHp, stageNm, stagePrice, stageMethod, stageDyno, sinfo, snote FROM TUNE_stages WHERE sId = '.$get_stage['sId']; 
       // debug($sql_dup_stage); 
       $qry_dup_stage = mysql_query($sql_dup_stage); 
      // 
      } 
     // 
    /**/ 
    } 
/**/ 

당신이 볼 수 있듯이, 마지막 방법을 사용했습니다.
-loop의 끝에 UPDATE을했습니다. 나는이 간단 할과 제안에 대해 열려있는 수 믿는다

...

답변

2

당신은 두 번째 업데이트가 필요하지 않습니다.

$sql_dup_stage = 'INSERT INTO TUNE_stages (cId, stage, stageHp, stageNm, stagePrice, stageMethod, stageDyno, sinfo, snote) 
      SELECT \''.$new_carId.'\', stage, stageHp, stageNm, stagePrice, stageMethod, stageDyno, sinfo, snote FROM TUNE_stages WHERE sId = '.$get_stage['sId'] 
+0

고마워 :에

$sql_dup_stage = 'INSERT INTO TUNE_stages (cId, stage, stageHp, stageNm, stagePrice, stageMethod, stageDyno, sinfo, snote) SELECT cId, stage, stageHp, stageNm, stagePrice, stageMethod, stageDyno, sinfo, snote FROM TUNE_stages WHERE sId = '.$get_stage['sId']; 

변경합니다. '$ new_carId'를'SELECT' 전에 넣고 그 안에 [cId]를 두지 않으려 고 시도했습니다. 하지만 그건 효과가 없었습니다. – ThomasK

+0

내 실수. 당신은 SQL에 문자열 값 주위에 따옴표를 넣어야한다. 나는 그것을 반영하기 위해 나의 대답을 편집 할 것이다. –

+0

첫 번째 게시물을 귀하의 제안으로 업데이트했습니다. 저는 같은 컴퓨터에 있지는 않지만이게 같았고 완벽하게 작동했다고 생각합니다 ... 다시 한 번 감사드립니다 ... – ThomasK