MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2000, 12:29 PM
gszczyrb gszczyrb is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Lancaster, PA, USA
Posts: 8 gszczyrb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have two tables in my database. In the first table (member) an autonumber is assigned (membernum) when info is submitted through the form. I'd like to be able to include this number (as a foreign key) in the second table (car) also. Here is what I tried without success:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

mysql_query ("INSERT INTO member (fname, lname)
VALUES ('$fname', '$lname')");

mysql_query ("INSERT INTO car (membernum, year, model)
VALUES (mysql_insert_id(), '$year', '$model')");
[/code]
Any ideas on how to do this? Also, how can I set the auto increment number to start with a higher number.

TIA, Greg

Reply With Quote
  #2  
Old February 10th, 2000, 02:56 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
insert_id doesn't work without placing a null in to it's place in the query (it's not like a timestamp), so you may need to specify that in the first query.

mysql_query ("insert into member (memid,fname,lname) values (null,'$fname','$lname')");

Then it should work. To start at a higher number for initial value, you'll need to force the number for the first record. What I've done is use a dummy record with a defined number. Later, after you have "real" records you can delete the dummy.


Reply With Quote
  #3  
Old February 15th, 2000, 08:29 AM
gszczyrb gszczyrb is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Lancaster, PA, USA
Posts: 8 gszczyrb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by rod k:
insert_id doesn't work without placing a null in to it's place in the query (it's not like a timestamp), so you may need to specify that in the first query.

mysql_query ("insert into member (memid,fname,lname) values (null,'$fname','$lname')");

Then it should work. To start at a higher number for initial value, you'll need to force the number for the first record. What I've done is use a dummy record with a defined number. Later, after you have "real" records you can delete the dummy.

[/quote]

Thanks. I tried that and I'm still having problems. The first table gets the data, but the second table doesn't. HEre is my code.

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
mysql_query ("INSERT INTO memtest (ffr, fname, lname)
VALUES (null, '$fname', '$lname')");

mysql_query ("INSERT INTO cartest (year, model)
VALUES (mysql_insert_id(), '$year', '$model')");

[/code]

I am still doing something wrong?


Reply With Quote
  #4  
Old February 15th, 2000, 10:49 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Yes, look at your second insert.. you've defined two columns and then assign 3 values!

Get in the habit of using

print mysql_error();

after any mysql query or connection. Will save a lot of debugging time

Rod

Reply With Quote
  #5  
Old February 15th, 2000, 02:59 PM
gszczyrb gszczyrb is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Lancaster, PA, USA
Posts: 8 gszczyrb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Well, I fixed what you said and it still wasn't working. I fixed it though. I used this instead :

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
LAST_INSERT_ID()
[/code]

This seems to work better. Thanks for all your help.

BTW, that statement,
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
print mysql_error();
[/code]
is really helpful. Thanks.

Reply With Quote
  #6  
Old February 16th, 2000, 04:30 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
DOH! Of course, can't use a php function inside the query. Using the mysql function in there works though.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > auto increment number


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway