Hi,
I have the following tables
tbl_user - id, username, password
tbl_userprofile - id, userid, name, surname, gender, nationality,address, mobile, department, email, question, answer.
userid is a foreign key in tbl_userprofile referencing id in tbl_user
I'm trying insert a new user into tbl_userprofile. But i'm not sure how to go about it because of the foreign key constraint. Here's my code.
$name = "";
$surname = "";
$address = "";
$nationality = "";
$gender = "";
$email = "";
$department = "";
$mobile = "";
$question = "";
$answer = "";
$errorMessage = "";
//check if form was submitted
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['firstname'];
$surname = $_POST['lastname'];
$email = $_POST['email'];
$address = $_POST['address'];
$mobile = $_POST['telno'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$nationality = $_POST['nationality'];
$department = $_POST['department'];
$gender = $_POST['gender'];
$username = "root";
$password = "root";
$database = "db";
$server = "host";
$db_handler = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database,$db_handler);
if($db_found){
$SQL = "INSERT INTO tbl_userprofile (userid, name, surname, gender, nationality, address, mobile, department, email, question, answer) VALUES
('$name','$surname','$gender','$nationality','$address','$mobile','$department','$email','$question','$answer')";
$result = mysql_query($SQL);
mysql_close($db_handler);
if($result){
header("Location:index.php");
}
else{
$errorMessage = "ERROR". mysql_error();
}
}
else{
$errorMessage = "Database Not Found";
}
}
I know my $SQL query is incomplete. How do i construct it? Thanks