Re: [PHP-DB] set primary key of a mysql-table automatically as value in other tables of the same db From: Dobromir Velev (diadomraz <email protected>)
Date: 11/30/01

Hi,
First you insert the record in the main table, then using the mysql_insert_id() finction (Check www.php.net/mysql_insert_id for
details) you retrieve the new auto_increment value and then you insert the records in the related tables.

your script should look something like this:

<?
$res=mysql_query("insert into main (id,field_one,...) values (null,'$field_one',...)",$db);
$id=mysql_insert_id():
mysql_query("insert into table_one (id_main,table_one_field_one,...) values ($id,$table_one_field_one,...)",$db);
....
mysql_query("insert into table_six (id_main,table_six_field_one,...) values ($id,$table_six_field_one,...)",$db);
?>

Regards
Dobromir Velev
Web Developer
www.websitepulse.com

-----Original Message-----
From: es said <essaid <email protected>>
To: php-db <email protected> <php-db <email protected>>
Date: Friday, November 30, 2001 12:55
Subject: [PHP-DB] set primary key of a mysql-table automatically as value in other tables of the same db

>how to set the primary key of a mysql-table automatically as value in other
>tables of the same db via php?
>
>hi there,
>as being a newbie in this list, i don't know if my problem already has being
>solved. so here it is:
>
>i created a database containing 6 tables: 'main' got the key 'id_main'
>(integer auto_increment), every other table got the field 'id_main'(integer)
>too, to connect all of them.
>so, if I enter a new set of data via a php-form, the value $id_main is
>automatically given by mysql. but how do I tell the program to enter the
>same value $id_main in all of the other tables, because the value is given
>in mysql and not via php...?
>(maybe it would be better to automatically get the id of the last set and
>add 1. if someone got a script for this, i would be happy, too!)
>hopefully i was able to explain my question properly and someone got an
>answer. have a nice day!
>thanx
>cornelia
>
>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe <email protected>
For additional commands, e-mail: php-db-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>