2011-07-26 5 views
0

저는 이것이 중복 될까 봐 걱정됩니다. 나는 부지런히 일해 왔으며 지금까지 발견 한 세 가지 이상을 대체했으며 아무것도 작동하지 않습니다.한 테이블에서 다른 테이블로 배열을 삽입하십시오.

목표는 두 테이블의 고유 필드를 병합하는 것이지만 지금은 동일한 테이블로 작성된 다른 필드로 동일한 필드를 가져올 수 없습니다. 지금까지 코드는 내 의견과 함께 :

$result = mysql_query("SELECT * from $db.$tbl LIMIT 50"); 

if($result) { 
while($maindata = mysql_fetch_assoc($result)) { 

$newdata = implode ("," , $maindata); 

$newdata = mysql_escape_string($newdata); 

$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks) 
SELECT FROM $db.address";   

//This is the original code replaced by the above 
//$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks) 
// VALUES ($newdata)"; 

mysql_query($pop); 

//print_r($pop); 
    //Seems to give the correct output in the browser, but the table address_new remains empty. ` 

감사합니다. 난 정말 당신의 도움을 주셔서 감사합니다.

+0

변화 쿼리 라인는 mysql_query'에 :

INSERT IGNORE INTO db.address_new (id,entitynum,address,city,state,zip,country,remarks) SELECT (id,entitynum,address,city,state,zip,country,remarks) FROM db.address LIMIT 50 

보조 노트로 : 개인적으로, 나는 하나에 고유 키 (또는 세트) 다음 값과 단지 INSERT IGNORE 할 수있을 것입니다 $ pop) 또는 die (mysql_error());를 다시 실행하고 오류가 발생하면 알려주십시오. –

+0

@ 마크 그는 분명히 MySQL 구문 오류를 만날 것입니다. – cwallenpoole

+0

그럼 <여기에 당신의 결말을 삽입하십시오>! db.address_new가 존재하지 않는다는 오류가 있습니다. 그러나, 그것은 mysqlcc를 사용하여 볼 수 있습니다. – CoffeeBean

답변

2

직접 다른 테이블에서 삽입하려면 (참고 : ID는 나에게 이런 식으로 삽입 할 수 없습니다 수 있습니다 AUTO_INCREMENT 경우) :

INSERT INTO db.address_new (id,entitynum,address,city,state,zip,country,remarks) 
    SELECT (id,entitynum,address,city,state,zip,country,remarks) 
     FROM db.address LIMIT 50 

(루프에 넣고하지 않음)

을 고유 한 가치를 찾고 있다면 두 가지 방법으로 할 수 있습니다. (

// use mysql_real_escape_string 
mysql_escape_string($newdata); 

// since you're passing this through PHP, you need to make sure to quote 
// all of your values. This probably means you'll need to loop and replace this 
// $newdata = implode ("," , $maindata); 
// with something like: 

$newdata = array(); 
foreach($maindata as $column) 
{ 
    $newdata[] = "'" . mysql_real_escape_string($column) . "'"; 
} 
$newdata = implode(',', $newdata); 


// you're missing the columns in your select clause. 
$pop = "INSERT INTO $db.address_new ". 
     "(id,entitynum,address,city,state,zip,country,remarks) ". 
     // You need to select *something* 
     "SELECT FROM $db.address"; 
+0

지금 당장 테이블을 찾는 데 어려움이 있습니다. 나가 운동 할 때 이것을 적용 할 것이다. 고맙습니다! – CoffeeBean

+0

감사합니다. 문제는 프로그램에서 이전에 데이터베이스 핸들을 설정 한 후 나중에 pconnect를 설정하는 데서 비롯된 것입니다. 바보 같은 실수입니다. $ dbh 핸들을 mysql_query의 매개 변수로 추가 한 후에 모든 것이 잘 작동했다. – CoffeeBean

관련 문제