0

I have two table in MySql, which are table 1 and table 2. As I want to link table 1 to 2 via userID. However, the funciton I have come out is not working.

MySQl tables are below:

enter image description here

enter image description here

In my case, userId will be the foreign key to link these 2 tables. However, my functions are not working. This is my function below:

function insert() {
    function adduserdetails($con, $accountId, $name, $contact) {

        $query = "insert into userdetails(accountId,name,contact) 
            values('$accountId','$name','$contact')";
        //echo "{$sqlString}";



        $insertResult = mysqli_query($con, $query);


        if ($insertResult) {
            echo " Applicant Detail Added !<br />";
            echo "<a href='index.php'>Back to Home</a>";
        } else {
            echo " Error !";
            echo "{$query}";
            //header('Location: post.php');
        }
    }

}

if ($con->query($query) === TRUE) {
    $last_id = $con->insert_id;
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $query . "<br>" . $con->error;
}

function adduseremployment($con,$last_id,$occupationMain,$comapny){

    $query1 = "insert into useremployment(userId,Occupation,company) 
            values('$last_id',$occupationMain','$comapny')";            
                 //echo "{$sqlString}";


                 $insertResult = mysqli_query($con, $query1);


                 if($insertResult){
                     echo " Applicant Detail Added !<br />";
                     echo "<a href='index.php'>Back to Home</a>";
                 }
                 else {
                     echo " Error !";
                     echo "{$query1}";
                     //header('Location: post.php');
                 }


}
xhinvis
  • 201
  • 4
  • 15

3 Answers3

0

In second insert you are using wrong column name (userID and not accountID)

   $query1 = "insert into useremployment(userID,Occupation,company) 
                                         ^^^^^^ here
          values('$last_id',$occupationMain','$comapny')";            

and in function signature and body you have comapny (could be you want company)

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • is the if function is correct? I tried to use 'if' to link $last_id to the userId in the userdetails table – xhinvis Jul 08 '17 at 06:10
  • If you have not the userID column in you insert statement you can't assign the correspondant var to the column .. the missing userID is inside the column name declaration ... asnwer updated with ^^^^here for position – ScaisEdge Jul 08 '17 at 06:13
0

You have vertical table.

Check below code,

Be remember to prepare statement.

<?php
    function addUser(){
        // parent table.
        $queryParent = ''; // Your userdetails table insert query
        $insertResult = mysqli_query($con, $queryParent);
        $userId = mysqli_insert_id($con); // This is your last inserted userId.

        // child table.
        $queryChild = ''; // Your useremployment table insert query with userId.
        $insertResult = mysqli_query($con, $queryChild);
    }
?>
Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39
0

your Query is wrong, please check this,

$query1 = "insert into useremployment(userId,occupation,company) 
        values('$last_id',$occupationMain','$comapny')";  

you have to store record on userId as a refrence not to "accountID".

hope it helps,

Ritesh Khatri
  • 1,253
  • 13
  • 29