2012-01-25 89 views
-1

我想用下面的函數複製我的MySQL記錄。它說我的SQL中有一個錯誤,我看不到。我在底部添加了一些回聲以幫助診斷。另外,是否有更簡單的方法來單獨命名每行? (行[$ X])?用AI ID字段複製MySQL記錄

該功能基本上是讀取發佈給它的任何ID,然後將其寫回數據庫,就好像用戶只是自己輸入它一樣,而不用按重複按鈕。

我已經嘗試了其他代碼在堆棧上,我想我會寫一個我真的理解,所以在這裏!

編輯:看來,我的第二個查詢也是有問題的。

謝謝!

function procDupRec(){ 

    $id = mysql_real_escape_string($_GET['duplicate']); 

    $query = "SELECT * FROM contacts WHERE id=$id"; 

      $result = mysql_query($query);  

      while($row = mysql_fetch_row($result)){ 

      $category = $row[1]; 
      $dateEntered = $row[2]; 
      $account = $row[3]; 
      $firstName = $row[4]; 
      $lastName = $row[5]; 
      $company = $row[6]; 
      $titlePos = $row[7]; 
      $officeNum = $row[8]; 
      $phoneExt = $row[9];   
      $homeNum = $row[10]; 
      $mobileNum = $row[11]; 
      $pagerNum = $row[12]; 
      $faxNum = $row[13]; 
      $address1 = $row[14]; 
      $address2 = $row[15]; 
      $city = $row[16]; 
      $state = $row[17]; 
      $zip = $row[18]; 
      $email = $row[19]; 
      $website = $row[20]; 
      $notes = $row[21]; 
      $editBit = $row[22];  
      $protection = $row[23]; 
      $lastIP = $_SERVER['REMOTE_ADDR']; 

      } 

      $query2 = mysql_query("INSERT INTO contacts (category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP) VALUES ('$category', '$dateEntered', '$account', '$firstName', '$lastName', '$company', '$titlePos', '$officeNum', '$phoneExt', '$homeNum', '$mobileNum', '$pagerNum', '$faxNum', '$address1', '$address2', '$city', '$state', '$zip', '$email', '$website', '$notes', '$editBit', '$protection', '$lastIP')"); 

      $result2 = mysql_query($query2); 

      echo $query2; 

      echo mysql_error(); 



} 

回答

1

執行查詢後,迴應查詢將不起作用。這隻會包含一個結果。

你應該執行它,這樣

$query2 ="INSERT INTO contacts (category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP) VALUES ('$category', '$dateEntered', '$account', '$firstName', '$lastName', '$company', '$titlePos', '$officeNum', '$phoneExt', '$homeNum', '$mobileNum', '$pagerNum', '$faxNum', '$address1', '$address2', '$city', '$state', '$zip', '$email', '$website', '$notes', '$editBit', '$protection', '$lastIP')"; 
mysql_query($query2); 

通過此之前存儲查詢,你可以檢查與echo $query2

另一種選擇查詢複製:

$query = "INSERT INTO contacts 
        (category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP) 

        (SELECT category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP FROM contacts WHERE id=$id)" 
mysql_query($query); 
+0

然後我得到一個「對於密鑰1」 –

+0

重複輸入「2852」是否爲ID行的auto_increment?因爲如果是,那麼ID字段應該被忽略 –

+0

auto_increment在 –