2012-09-25 3 views
0

가능한 중복 :
PHP: how to get last inserted ID of a table?AUTO_INCREMENT 2의 테이블

내가 양식을 만들어 두 테이블에 데이터를 기록 할 수 있습니다.

$sql = "INSERT INTO ma_forum (notcat_id, usr_id, not_titulo, not_status, not_tags, not_data, not_comentarios, not_resumo, not_texto) 

     VALUES('$categoria', '".$_SESSION[usr_name]."', '$titulo', '$status', '$tags', '".time()."', '$comentarios', '$resumo', '$texto')"; 

$res = mysql_query($sql) or die(mysql_error()); 

$sql_or = "INSERT INTO ma_forum_comentarios (comnot_habbo, comnot_data, comnot_status) VALUES('$_SESSION[usr_name]', '".time()."', '$status_comnot')"; 

$res = mysql_query($sql_or) or die(mysql_error()); 

그러나 첫 번째 표에는 auto_increment ID가 자동으로 기록됩니다. 하지만 첫 번째 테이블과 다른 열의 ID를 입력하고 싶습니다. 누구나 할 줄 알아?

답변

1

마지막 ID가 삽입 된 검색 PHP 함수 인 mysql_insert_id()를 사용하여 http://php.net/manual/en/function.mysql-insert-id.php

그래서 그것은해야한다 :

$sql = "INSERT INTO ma_forum (notcat_id, usr_id, not_titulo, not_status, not_tags, not_data, not_comentarios, not_resumo, not_texto) VALUES ('$categoria', '".$_SESSION[usr_name]."', '$titulo', '$status', '$tags', '".time()."', '$comentarios', '$resumo', '$texto')"; 

$res = mysql_query($sql) or die(mysql_error()); 

$sql_or = "INSERT INTO ma_forum_comentarios (id, comnot_habbo, comnot_data, comnot_status) VALUES(" . mysql_insert_id() . ",'$_SESSION[usr_name]', '".time()."', '$status_comnot')"; 

$res = mysql_query($sql_or) or die(mysql_error()); 

당신은 내가 실제 ID로 지정된 'ID'열을 교체해야합니다 열 이름.

0
mysql_query("INSERT INTO mytable (product) values ('kossu')"); 
printf("Last inserted record has id %d\n", mysql_insert_id()); 
관련 문제