How can one ensure that the $_POST variable is not empty when using it in a SQL query in PHP?

When using the $_POST variable in a SQL query in PHP, it's important to ensure that the variable is not empty to avoid potential SQL injection vulnerabilities. One way to achieve this is by checking if the $_POST variable is set and not empty before using it in the query. This can be done using the isset() and !empty() functions in PHP.

if(isset($_POST['variable_name']) && !empty($_POST['variable_name'])) {
    $variable = $_POST['variable_name'];
    // Use $variable in SQL query
} else {
    // Handle the case when the $_POST variable is empty
}