| | |
Get ID from table
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 42
Reputation:
Solved Threads: 0
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.
can anyone assist me thru my code if i have done some terrible mistake here. Thank you so much.
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)
$orange_insert = "INSERT INTO orange (orange_id, orange_name, orange_add) VALUES (NULL, '$val_name', '$val_add')"; $result = mysql_query($orange_insert) OR die (mysql_error()); $orange_sel = "SELECT * FROM orange"; $result_orgsel = mysql_query($orange_sel) OR die (mysql_error()); $row = mysql_fetch_array($result_orgsel); $result_orange = $row['$orange_id']; echo $result_orange;
can anyone assist me thru my code if i have done some terrible mistake here. Thank you so much.
-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.,
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.
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.
should have been
Cheers!
Ie.,
MySQL Syntax (Toggle Plain Text)
$orange_insert = "INSERT INTO orange (orange_name, orange_add) VALUES ('$val_name', '$val_add')";
•
•
•
•
error_reporting = E_ALL & ~E_NOTICE
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)
$result_orange = $row['$orange_id'];
MySQL Syntax (Toggle Plain Text)
$result_orange = $row['orange_id'];
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2009
Posts: 42
Reputation:
Solved Threads: 0
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
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
-1
#6 26 Days Ago
I am little confused about this part.
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
•
•
•
•
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.
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*
*PM asking for help will be ignored*
![]() |
Similar Threads
- ASP PERL script table (Perl)
- I have a problem in my table (PHP)
- Display Table From MySQL DB (C#)
- Dreamweaver MX - unwanted white table space at top (Graphics and Multimedia)
Other Threads in the MySQL Forum
- Previous Thread: SQL Query
- Next Thread: How to store a big database
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contractors copyright count court crm data database design developer development distinct drupal dui ec2 email enter enterprise eudora facebook form foss gartner government gpl greenit groklaw groupware hiring hyperic innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization






