mishuk Posted February 8, 2007 Share Posted February 8, 2007 I am having trouble with a peice of code that will take the ethnicity string and replace it with the id number allocated to it and insert the id number into the database. Here is the code $ethnicity = mysql_query("SELECT ethnic_id FROM tbl_ethnicity WHERE ethnicity = \"White British\""); $addstudent = "INSERT INTO tbl_student (ethnic_id) VALUES ('$ethnicity')"; $result = mysql_query($addstudent) or die(mysql_error()); I placed this statement below and it tells me that $ethnicity = 2 which is correct. however no value is submitted to the database. Do you have any ideas why this is? while($row=mysql_fetch_array($ethnicity)) { echo($row["ethnic_id"]."<br>");} I will eventually replace the \"White British\" with a variable taken from an input form however am taking this one step at a time. Any help would be great Thanks Link to comment https://forums.phpfreaks.com/topic/37613-record-into-database-problem/ Share on other sites More sharing options...
hvle Posted February 8, 2007 Share Posted February 8, 2007 $ethnicity = 2 is NOT correct. should do something like below $ethnicity = mysql_query("SELECT ethnic_id FROM tbl_ethnicity WHERE ethnicity = \"White British\""); $row = mysql_fetch_array($ethinicity); $addstudent = "INSERT INTO tbl_student (ethnic_id) VALUES ('{$row['ethnic_id']})"; $result = mysql_query($addstudent) or die(mysql_error()); $ethnicity is a resource Link to comment https://forums.phpfreaks.com/topic/37613-record-into-database-problem/#findComment-179869 Share on other sites More sharing options...
mishuk Posted February 8, 2007 Author Share Posted February 8, 2007 Many thanks Link to comment https://forums.phpfreaks.com/topic/37613-record-into-database-problem/#findComment-179876 Share on other sites More sharing options...
hvle Posted February 8, 2007 Share Posted February 8, 2007 no problemo Link to comment https://forums.phpfreaks.com/topic/37613-record-into-database-problem/#findComment-179884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.