php file generating and reading data from a database

There is a function that generalize a php file:

require_once "connection.php";

// Módosítások elmentése vagy új termék hozzáadása
// Módosítások elmentése vagy új termék hozzáadása
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $new_name = isset($_POST["new_name"]) ? $_POST["new_name"] : "";
    $new_price = isset($_POST["new_price"]) ? $_POST["new_price"] : "";
    $new_image_url = isset($_POST["new_image_url"]) ? $_POST["new_image_url"] : "";

    if (isset($_POST["product_id"])) {
        /
        $product_id = $_POST["product_id"];
        updateProduct($product_id, $new_name, $new_price, $new_image_url);
    } else {
        
        $new_product_name = isset($_POST["new_product_name"]) ? $_POST["new_product_name"] : "";
        $new_price = isset($_POST["new_price"]) ? $_POST["new_price"] : "";
        $new_image_url = isset($_POST["new_image_url"]) ? $_POST["new_image_url"] : "";
        
        $new_product_id = generateSProductFile($new_product_name, $new_price, $new_image_url);
    }
}
function generateSProductFile($name, $price, $image_url) {
    global $conn;

   
    $insert_query = "INSERT INTO product (product_name, price, picture) VALUES (?, ?, ?)";
    $stmt = mysqli_prepare($conn, $insert_query);
    mysqli_stmt_bind_param($stmt, "sds", $name, $price, $image_url);
    mysqli_stmt_execute($stmt);

    /
    $new_product_id = mysqli_insert_id($conn);

    $file_path = "sproduct_" . $new_product_id . ".php";
    $file_content = "<?phpn";
    $file_content .= "$product_id = $new_product_id;n"; 
    $file_content .= "$product_name = "$name";n";
    $file_content .= "$product_price = "$price";n";
    $file_content .= "$product_picture = "$image_url";n";
    $file_content .= "?>n";
    $file_content .= file_get_contents("sproduct_template.php");
    
    
    file_put_contents($file_path, $file_content);

    return $new_product_id;
}

And there is the template file:


<?php
require_once "connection.php";




    
    $sql_product = "SELECT picture, product_name, price FROM product WHERE product_id = (SELECT MAX(product_id) FROM product)";
    $result_product = mysqli_query($conn, $sql_product);

    if ($result_product && mysqli_num_rows($result_product) > 0) {
        $row_product = mysqli_fetch_assoc($result_product);
        
        
        $product_name = $row_product['product_name'];
        $product_price = $row_product['price'];
        $product_picture = $row_product['picture'];
    } else {
        
        $product_name = "N/A"; 
        $product_price = "N/A";
        $product_picture = "default_image.jpg"; 
    
} else {
    
    $product_name = "N/A"; 
    $product_price = "N/A";
    $product_picture = "default_image.jpg"; 
}
?>

In addition, I also have a natural database, with the product table with manually entered products. The problem is that the newly generated php file shows incorrect product data. For some reason, it takes the data from the beginning of the database.
For example: I created a new product, and the database displayed the data of the product with ID 1 on the website.