How can I ensure that an entry is only made in a database if the PIN and username exist in another table in PHP?

To ensure that an entry is only made in a database if the PIN and username exist in another table in PHP, you can first query the table to check if the username and PIN combination exists. If it does, then proceed with the database entry. If not, display an error message or take appropriate action.

// Assuming $username and $pin are the values to be checked
// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check if username and PIN exist in another table
$query = "SELECT * FROM users WHERE username = '$username' AND pin = '$pin'";
$result = $conn->query($query);

if ($result->num_rows > 0) {
    // Username and PIN combination exist, proceed with database entry
    // Insert data into database
    $insertQuery = "INSERT INTO your_table_name (column1, column2) VALUES ('$value1', '$value2')";
    $conn->query($insertQuery);
} else {
    // Username and PIN combination do not exist, display error message or take appropriate action
    echo "Invalid username or PIN";
}

// Close database connection
$conn->close();