how to insert foreign key value not id through form in php

Avatar
  • Answered
my one table (user) contains attributes user_id|user_name|address|phone_no|designation_id | where designation_id is a foreign key and my 2nd table (designation) contains attributes designation_id|designation_name| i want to insert new entries through form and want to enter foreign key(designation_id) value not id so that this value stores in designation table.My code for insert new data is this but it only takes designation_id not value.
if(isset($_POST['user_name']))
{
$user_name=$_POST['user_name'];
$address=$_POST['address'];
$phone_no=$_POST['phone_no'];
$designation_id=$_POST['designation_id'];
if(mysql_query("insert into user(user_id,user_name,address,phone_no,designation_id) values ('','$user_name','$address','$phone_no','$designation_id')"))
echo "successful insertion!";
else
echo"please try again";
}
$res=mysql_query("select * from user");
what should i do so that my insert foreign key value not id,it save in designation table.
Avatar
-1
JeffMa
Hello, A foreign key will reference the ID of the parent table so it should only be sent that parent table's ID when submitting the form. As long as the table's foreign key matches up with the primary key of the parent table, everything should go smootly. Be sure that your tables are configured correctly for foreign keys. If you need more information on foreign keys, I was able to locate a great tutorial for you: MySQL Foreign Keys Best regards, JeffMa