I have a database that consists of 3 tables.
user: 4 columns userId [primary key], name, emailId, password
residentController: 3 columns userId [foreign], departmentId [foreign], checkDefault checkDefault
will be set to 1.
department with 6 columns: departmentId [primaryKey], departmentName, departmentDesc, departmentLink, checkDefault, departmentCategory
Here is the sequence that follows when a user registers for an android application. The following php script will be called.
It will add a record of the user into the database. After insertion of the data in the user
table, I want to get all the "departmentNames" from department
table and insert it into residentController
. In the end, after particular user registers, then residentController must have these values populated into the residentController table.
userId departmentId checkDefault
---------------------------------
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
Here is my php script.
<?php
require "init.php";
$fullname = "Fahman Khan";//$_POST["user"];
$emailid = "fxk120@hotmail.com";//$_POST["user_name"];
$userpass = "Windows";//$_POST["user_pass"];
$sql_query = "insert into user values(NULL,'$fullname','$emailid','$userpass')";
if(mysqli_query($connection, $sql_query))
{
echo "<h2> Data insertion success</h2>";
//Get the user id of the the registered user
$sql_queryTwo = "SELECT userId FROM user WHERE emailId='".$emailid."'";
//Store the result of the query in a variable
$result= mysqli_query($connection,$sql_queryTwo);
$row = mysqli_fetch_array($result);
//Retrive the user id
$data = $row[0];
//Insert default departments into residentController
}
else
{
//echo "Data insertion error".mysqli_error($connection);
}
?>
My question is that in my current php script, I don't know how I can insert departmentID into residentController table. departmentId is a foreign key and in my current department table there are 5 departments. I just need help writing the part in my php where I can insert in values into my residentController table. As of now, the current php script only adds a new user, but I need help inserting values into my residentController table.