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.

$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.

2
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by futhonguy
0

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.,

$orange_insert = "INSERT INTO orange (orange_name, orange_add)
	             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.

$result_orange = $row['$orange_id'];

should have been

$result_orange = $row['orange_id'];

Cheers!

0

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

0

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/en/innodb-foreign-key-constraints.html

0

Thanks again nav33n. I'll give it a try and also access to the link for reference.
Let u know the result soon. Cheers!! :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.