944,029 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 861
  • MySQL RSS
Nov 10th, 2009
0

Get ID from table

Expand Post »
Hi,

I am trying to get the primary key (which is number) from a field of the same table. however i encountered "Undefined index" shown on my screen.

MySQL Syntax (Toggle Plain Text)
  1. $orange_insert = "INSERT INTO orange (orange_id, orange_name, orange_add)
  2. VALUES (NULL, '$val_name', '$val_add')";
  3. $result = mysql_query($orange_insert) OR die (mysql_error());
  4. $orange_sel = "SELECT *
  5. FROM orange";
  6. $result_orgsel = mysql_query($orange_sel) OR die (mysql_error());
  7. $row = mysql_fetch_array($result_orgsel);
  8.  
  9. $result_orange = $row['$orange_id'];
  10.  
  11. echo $result_orange;

can anyone assist me thru my code if i have done some terrible mistake here. Thank you so much.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
futhonguy is offline Offline
69 posts
since Oct 2009
Nov 11th, 2009
0
Re: Get ID from table
One suggestion. When you are inserting a record to the table, you don't have to insert 'NULL' if its an auto increment field. Mysql will do it for you.
Ie.,
MySQL Syntax (Toggle Plain Text)
  1. $orange_insert = "INSERT INTO orange (orange_name, orange_add)
  2. VALUES ('$val_name', '$val_add')";
Secondly, the 'error' you are getting is not exactly an error, but a notice. You get these notices if you use a variable without initializing it. You can turn off the notices by changing your php.ini file. Change error reporting in php.ini to the following if you want to disable notices.
Quote ...
error_reporting = E_ALL & ~E_NOTICE
This means, show all errors except notices.
If you don't want to disable notices, then start initializing all your variables before using it.
Finally, You have an error in your code.
MySQL Syntax (Toggle Plain Text)
  1. $result_orange = $row['$orange_id'];
should have been
MySQL Syntax (Toggle Plain Text)
  1. $result_orange = $row['orange_id'];
Cheers!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Nov 11th, 2009
0
Re: Get ID from table
Got it!
Cool thanks mate..
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
futhonguy is offline Offline
69 posts
since Oct 2009
Nov 11th, 2009
0
Re: Get ID from table
You are welcome.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Nov 11th, 2009
0
Re: Get ID from table
However nav33n, I have got this problem struck for days and not able to solve it. Not sure if you can help as well. Basically I have 2 tables (A and B) each has their own ids and fields. somehow i would like to join up A and B. I understand that's a 'select - join' function to use for this case. But what Im trying to do is having the user to enter values into Table B where the id of Table B will automatically appear onto Table A. There will be no duplication on Table B.

For this case, am i suppose to set up the relationship at the begining when i created the table? If so what is the code for it?
Sorry to touble u again.

cheers
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
futhonguy is offline Offline
69 posts
since Oct 2009
Nov 11th, 2009
0
Re: Get ID from table
I am little confused about this part.
Quote ...
But what Im trying to do is having the user to enter values into Table B where the id of Table B will automatically appear onto Table A.
If I get it right, When you insert a record to table B, you can get the id by using mysql's last_insert_id or php's mysql_insert_id. You can then insert a record in table A using this value.
You can make id column of both the tables A and B as primary key (since no duplicates are allowed). Then, you can have a column in table A (for example, tableB_id) and make it a foreign key referencing table B's id column. You can add constraints after creating a table. Thats not a problem!
http://dev.mysql.com/doc/refman/5.1/...nstraints.html
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Nov 11th, 2009
0
Re: Get ID from table
Thanks again nav33n. I'll give it a try and also access to the link for reference.
Let u know the result soon. Cheers!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
futhonguy is offline Offline
69 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: SQL Query
Next Thread in MySQL Forum Timeline: How to store a big database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC