What security considerations should be taken into account when dynamically creating tables based on user input in PHP?

When dynamically creating tables based on user input in PHP, it is important to sanitize and validate the input to prevent SQL injection attacks. Additionally, it is crucial to limit the privileges of the database user executing the queries to only necessary permissions to reduce the potential impact of an attack. Lastly, consider implementing input validation to ensure that only allowed characters and table names are used.

// Sanitize and validate user input for table name
$tableName = filter_var($_POST['tableName'], FILTER_SANITIZE_STRING);

// Limit database user privileges to only necessary permissions
// For example, use a database user with limited CREATE TABLE privileges

// Implement input validation to only allow certain characters in table name
if (!preg_match('/^[a-zA-Z0-9_]+$/', $tableName)) {
    // Handle invalid table name input
}