 |
| PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 19th, 2004, 04:18 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
write to 2 tables using same id.
 I have been working on this problem for 2 days solid and my head hurts!
I am writing a newsletter where users are able to write the article and for the information to be written to 2 tables in the database. I can do this for one table but it need it for 2 tables using PHP and MySQL.
Obviously what needs to happen if that the two ids from t1 and t2 be linked together but how do i get the code to tell it to write to both tables using the same id??
Does anyone know how this would be done?
All comments appreciated.
Michael.
__________________
Michael.
|
|

July 19th, 2004, 04:37 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I don't quite see why you aren't using tow INSERT statements, since you can only insert into one table at a time, but if what you mean is that you want to insert into one table, which assigns a primary key id to the newly inserted record because it is an AUTO_INCREMENT field, and then want to insert that automatically generated value into the secobnd table, then all you have to do, is carry out your first insert, and then make a call to:
$id = mysql_insert_id($connection);
$connection is the variable which stores you connection to the DB. As with all such PHP functions, it is optional, in that, if you are only using the one connection, PHP will assume you mean that connection. The function will return the last value automatically generated. You can then use that in you second insert.
Does that help? If not, can you explain what you're trying to do in some more detail, perhaps.
Take it easy,
Dan
|
|

July 19th, 2004, 04:48 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi there Dan,
Thanks for the reply.
I'm a newbie to the PHP MySQL domain and I know they are very good combos. I guess I thought that you could write to 2 tables using one SQL statement just like you can extract several tables using one call just like the SELECT table1.field3 table2.field3 FROM table1, table2 WHERE myquery;
I'll give your suggestion a go and will let you know how I got on.
Michael.
|
|

July 19th, 2004, 05:53 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
:)Hi Dan,
Just to let you know that your reply has worked a treat!
Thank you.. answer so simple! DOH.
Michael.
|
|

July 20th, 2004, 10:31 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Returning back to this topic, after a closer inspection and an empty of the table data, the SQL statement,
Code:
INSERT INTO $table_newsitems_options
(newsitems_id, prod_id, internet, paper, archive)
VALUES
('$f_t2_id','$f_prod_id','$f_internet','$f_paper','$f_archive')
now no longer copies $f_t2_id to the newsitems_id.
Where $f_t2_id is taken from $f_t2_id = mysql_insert_id($connection);.
I have this very same similar problem with another bit of code like this.
Any ideas?
Thanks.
Michael.
|
|

July 22nd, 2004, 09:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Not really. So what does happen?
Dan
|
|
 |