multi-table insert

multi-table insert

am 28.04.2006 11:48:04 von Eustace

------=_NextPart_000_008C_01C66AB9.9BF66CB0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello everybody!
I am very much a newbie in PHP, but enjoying the learning process. Here and
there I get tangled in the logic of certain problems. Anyway, I have a
database about interns and this database has multi-tables told data of
interns, for example personal information, education qualifications,
computer skills, languages etc. The relationship of the tables is based on
the intern's username.
What I am trying to do is have a form, which an intern can fill in so that
details are inserted into the database. Since I am populating a number of
tables in one go, what's the best way to implement this? The main table is
the personal information one, which has the username as primary key, and the
rest of the tables username is the foreign key. Obviously I need the to pick
the username from the main table and insert it into the other tables. How
best can I do this?

Sorry this is a bit like an essay, but please assist. I know it looks
obvious to the experienced guys, but am having a bit of a problem. Thanks

Eustace

------=_NextPart_000_008C_01C66AB9.9BF66CB0--

Re: multi-table insert

am 01.05.2006 03:23:21 von Chris

Eustace wrote:
> Hello everybody!
> I am very much a newbie in PHP, but enjoying the learning process. Here and
> there I get tangled in the logic of certain problems. Anyway, I have a
> database about interns and this database has multi-tables told data of
> interns, for example personal information, education qualifications,
> computer skills, languages etc. The relationship of the tables is based on
> the intern's username.
> What I am trying to do is have a form, which an intern can fill in so that
> details are inserted into the database. Since I am populating a number of
> tables in one go, what's the best way to implement this? The main table is
> the personal information one, which has the username as primary key, and the
> rest of the tables username is the foreign key. Obviously I need the to pick
> the username from the main table and insert it into the other tables. How
> best can I do this?

No databases will do this automatically for you (whether you use mysql,
sqlite, postgresql or something else), so you need to create multiple
queries:

mysql example:

$query = "insert into users(username) values ('my_username')";
mysql_query($query);
$userid = mysql_insert_id();

$query = "insert into table2(userid) values('$userid')";
.....

postgres example:

$query = "select nextval('user_sequence') AS nextid";
$result = pg_query($query);
$row = pg_fetch_assoc($result);
$userid = $row['nextid'];

$query = "insert into table2(userid) values('$userid')";
.....

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: multi-table insert

am 02.05.2006 10:44:07 von Eustace

Thanks Chris!
Appreciate the help!

Eustace

-----Original Message-----
From: Chris [mailto:dmagick@gmail.com]
Sent: Monday, May 01, 2006 3:23 AM
To: eustace@safdem.org.zw
Cc: php-db@lists.php.net
Subject: Re: [PHP-DB] multi-table insert

Eustace wrote:
> Hello everybody!
> I am very much a newbie in PHP, but enjoying the learning process.
> Here and there I get tangled in the logic of certain problems. Anyway,
> I have a database about interns and this database has multi-tables
> told data of interns, for example personal information, education
> qualifications, computer skills, languages etc. The relationship of
> the tables is based on the intern's username.
> What I am trying to do is have a form, which an intern can fill in so
> that details are inserted into the database. Since I am populating a
> number of tables in one go, what's the best way to implement this? The
> main table is the personal information one, which has the username as
> primary key, and the rest of the tables username is the foreign key.
> Obviously I need the to pick the username from the main table and
> insert it into the other tables. How best can I do this?

No databases will do this automatically for you (whether you use mysql,
sqlite, postgresql or something else), so you need to create multiple
queries:

mysql example:

$query = "insert into users(username) values ('my_username')";
mysql_query($query); $userid = mysql_insert_id();

$query = "insert into table2(userid) values('$userid')"; ....

postgres example:

$query = "select nextval('user_sequence') AS nextid"; $result =
pg_query($query); $row = pg_fetch_assoc($result); $userid = $row['nextid'];

$query = "insert into table2(userid) values('$userid')"; ....

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.5.1/328 - Release Date: 5/1/2006

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php