How to get last auto increment value as foreign key to another table in php
<p>How to get data from one table to the foreign key of another table in object  oriented php, i have two table one called <strong>users</strong> which has two fields : id and phonenumber . another table is <strong>profile</strong> which has id,user_id profile_image, username, businessname and town where user_id is a foreign key of id in users table . and i wish i insert phone number in users table i get foreign key of user_id in profile table . please i need your help i am a beginer in oophp . Thanks any help will be greatly appreciated</p>

<p><strong>Dbhandler.php which handles all functions</strong></p>

<pre><code>&lt;?php
class DbHandler {
    private $conn;

    function __construct() {
        require_once dirname(__FILE__) . '/DbConnect.php';
        // opening db connection
        $db = new DbConnect();
        $this-&gt;conn = $db-&gt;connect();
    }

    public function createUser($phone) {
        $response = array();

        // insert query
        $stmt = $this-&gt;conn-&gt;prepare("INSERT INTO users(phone) values(?)");
        $stmt-&gt;bind_param("s",$phone );

        $result = $stmt-&gt;execute();

        $new_profile_id = $stmt-&gt;insert_id;

        $stmt-&gt;close();

        // Check for successful insertion
        if ($result) {
            $profile_result = $this-&gt;createProfile_userId($new_profile_id);

            // User successfully inserted
            return USER_CREATED_SUCCESSFULLY;
        } else {
            // Failed to create user
            return USER_CREATE_FAILED;
        }
    }
    return $response;
}


public function createProfile_userId($user_id) {
    //getUser's FNK
    $stmt = $this-&gt;conn-&gt;prepare("DELETE FROM profile where user_id = ?");
    $stmt-&gt;bind_param("i", $user_id);
    $stmt-&gt;execute();
    $result = $stmt-&gt;execute();

    $stmt-&gt;close();
    return $result;
}

public function createProfile($profile_image, $username, $businessname,$town) {

    $response = array();

    $stmt =$this-&gt;conn-&gt;query("SELECT  profile_id FROM profile ORDER BY profile_id ASC") ;

    $profile_id = 0;

    while($row = $stmt-&gt;fetch_array()){
        $profile_id = $row['profile_id'];
    }

    $path = "uploads/$profile_id.png";
    $actualpath = "https://localhost/GoodPriceApi/uploadImage/$path";

    // insert query 
    $stmt = $this-&gt;conn-&gt;prepare("INSERT INTO profile(profile_image, username, businessname, town) values(?, ?, ?, ?)");
    $stmt-&gt;bind_param("isss",$profile_image, $username, $businessname, $town);

    $result = $stmt-&gt;execute();

    $stmt-&gt;close();

    // Check for successful insertion
    if ($result) {
        file_put_contents($path,base64_decode($image));

        // User successfully inserted
        return USER_CREATED_SUCCESSFULLY;
    } else {
        // Failed to create user
        return USER_CREATE_FAILED;
    }

    return $response;
}
?&gt;
</code></pre>

<p>The following file is used to call function. </p>

<p><strong>create.php</strong></p>

<pre><code>&lt;?php 
include './DbHandler.php';
$db = new DbHandler();

$response = array();

if ((isset($_POST['profile_image']) &amp;&amp; isset($_POST['username']) &amp;&amp;       isset($_POST['businessname']) &amp;&amp; isset($_POST['town']))!= '') {
    $profile_image = $_POST['profile_image'];
    $username = $_POST['username'];
    $businessname = $_POST['businessname'];
    $town = $_POST['town'];

    $res = $db-&gt;createProfile($profile_image, $username, $businessname,$town);

    if ($res == USER_CREATED_SUCCESSFULLY) {      
        $response["error"] = false;
        $response["message"] = "Profile created successfully";
    } else if ($res == USER_CREATE_FAILED) {
        $response["error"] = true;
        $response["message"] = "Sorry! Error occurred during profile creation.";
    } 
} 
?&gt;
</code></pre>