Get ID from table

Reply

Join Date: Oct 2009
Posts: 42
Reputation: futhonguy is an unknown quantity at this point 
Solved Threads: 0
futhonguy futhonguy is offline Offline
Light Poster

Get ID from table

 
0
  #1
26 Days Ago
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
-1
  #2
26 Days Ago
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.,
  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.
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.
  1. $result_orange = $row['$orange_id'];
should have been
  1. $result_orange = $row['orange_id'];
Cheers!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 42
Reputation: futhonguy is an unknown quantity at this point 
Solved Threads: 0
futhonguy futhonguy is offline Offline
Light Poster
 
0
  #3
26 Days Ago
Got it!
Cool thanks mate..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
-1
  #4
26 Days Ago
You are welcome.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 42
Reputation: futhonguy is an unknown quantity at this point 
Solved Threads: 0
futhonguy futhonguy is offline Offline
Light Poster
 
0
  #5
26 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
-1
  #6
26 Days Ago
I am little confused about this part.
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
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 42
Reputation: futhonguy is an unknown quantity at this point 
Solved Threads: 0
futhonguy futhonguy is offline Offline
Light Poster
 
0
  #7
26 Days Ago
Thanks again nav33n. I'll give it a try and also access to the link for reference.
Let u know the result soon. Cheers!!
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the MySQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC