Issue with foreign key constraint in PHP and MySQL script

I’m facing an issue with my PHP and MySQL script where I have a foreign key constraint between two tables. I’m unsure whether the issue lies in my index.php script or the database.sql script. I would appreciate any insights or suggestions to help me resolve this problem.

Description:
I have a web application where users can shorten URLs. I have two tables: “users” and “url_mappings”. The “users” table stores user information, and the “url_mappings” table stores the shortened URLs along with the corresponding user ID as a foreign key.

Problem:
When trying to insert data into the “url_mappings” table, I’m encountering the following error:
“Execution of prepared statement failed: Cannot add or update a child row: a foreign key constraint fails (tenazped_url_shortener.url_mappings, CONSTRAINT url_mappings_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id))”

Code snippets:
Here’s a simplified version of the relevant code in my index.php script:

<?php
// Relevant code for URL mapping insertion
// ...

$conn = connectToDatabase();
$stmt = $conn->prepare("INSERT INTO url_mappings (short_url, original_url, user_id) VALUES (?, ?, ?)");

// ...
?>

And here’s a snippet from my database.sql script:

-- Tabel users (users)
CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL,
    is_admin TINYINT(1) DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Tabel URL Shortener (url_mappings)
CREATE TABLE url_mappings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    short_url VARCHAR(255) NOT NULL,
    original_url VARCHAR(255) NOT NULL,
    user_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

Steps taken:
I have verified that the “users” table has valid data, including proper values in the “id” column.
I have checked the code in the index.php script that handles the insertion into the “url_mappings” table, ensuring that the user ID being used exists in the “users” table.
I have examined the database.sql script to confirm that the foreign key constraint is correctly defined between the two tables.

Expected Outcome:
I expect the insertion of data into the “url_mappings” table to succeed without any foreign key constraint errors.

Request:
Could you please help me identify the possible cause of this issue? Is there something I missed in my code or database structure? Any suggestions or insights would be greatly appreciated.
Thank you in advance for your assistance.