Jump to content

record into database problem


mishuk

Recommended Posts

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

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.