What best practices can be followed to improve error reporting and handling in PHP scripts dealing with form submissions and database interactions?

Issue: To improve error reporting and handling in PHP scripts dealing with form submissions and database interactions, it is important to utilize proper error handling techniques such as try-catch blocks and error logging.

try {
    // Database connection and query execution
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
    $stmt->bindParam(':email', $_POST['email']);
    $stmt->execute();
    
    // Form validation and submission
    if ($stmt->rowCount() > 0) {
        // Handle existing user
    } else {
        // Insert new user into database
    }
} catch (PDOException $e) {
    // Log the error message
    error_log("Database error: " . $e->getMessage());
    // Display a user-friendly error message
    echo "An error occurred. Please try again later.";
}