Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
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
 
Old July 19th, 2004, 04:18 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
 
Old July 19th, 2004, 04:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 19th, 2004, 04:48 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old July 19th, 2004, 05:53 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:)Hi Dan,
Just to let you know that your reply has worked a treat!

Thank you.. answer so simple! DOH.

Michael.
 
Old July 20th, 2004, 10:31 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old July 22nd, 2004, 09:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
Any ideas?
Not really. So what does happen?

Dan




Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert 2 tables with same auto increment ID ibrhaim ASP.NET 1.0 and 1.1 Basics 1 December 7th, 2006 05:00 PM
Write Nested XML document into SQL Server tables boondocksaint20 VB.NET 0 May 2nd, 2006 12:35 PM
Hide Tables by Id itHighway Javascript How-To 4 April 23rd, 2005 12:28 AM
Write Conflict message with SQL Server tables mmcdonal Access 3 November 18th, 2004 11:05 AM
why not index.asp?id=1 can be www.myweb.com/?id=1 BurhanKhan Classic ASP Professional 11 September 6th, 2004 02:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.