### Instruction:
Develop a PHP web application that displays a form allowing users to enter their name, age, and favorite color. Upon submission, store the data in a MySQL database table named "users". Validate the user's age to be between 18 and 99, and ensure the favorite color is one of the following: red, blue, green, yellow, or purple. Display an error message if validation fails, otherwise display a success message.


### Response:

To create a PHP web application that meets the requirements, follow these steps:

1. Set up your development environment: Install XAMPP or any other local server software to run PHP and MySQL on your computer.

2. Create a new folder named "user_form" in the htdocs folder of your XAMPP installation (usually located at C:\xampp\htdocs).

3. Inside the "user_form" folder, create two files: index.php and config.php.

4. Open the config.php file and add the following code to set up the database connection:

```php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_form_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>
```

5. Open phpMyAdmin from your XAMPP control panel and create a new database called "user_form_db". Then, create a table named "users" with the following columns: id (INT, AUTO_INCREMENT), name (VARCHAR), age (INT), favorite_color (VARCHAR). Set 'id' as the primary key.

6. Now open the index.php file and add the following code:

```php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Form</title>
</head>
<body>
    <?php
    require_once 'config.php';

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST['name'];
        $age = $_POST['age'];
        $favorite_color = $_POST['favorite_color'];

        $errors = [];

        if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
            $errors[] = "Only letters and white space allowed in name";
        }

        if ($age < 18 || $age > 99) {
            $errors[] = "Age must be between 18 and 99";
        }

        $allowed_colors = ['red', 'blue', 'green', 'yellow', 'purple'];
        if (!in_array($favorite_color, $allowed_colors)) {
            $errors[] = "Invalid favorite color. Allowed colors: red, blue, green, yellow, purple";
        }

        if (empty($errors)) {
            $stmt = $conn->prepare("INSERT INTO users (name, age, favorite_color) VALUES (?, ?, ?)");
            $stmt->bind_param("sis", $name, $age, $favorite_color);
            $stmt->execute();

            echo "<p>Success! Data has been saved.</p>";
        } else {
            echo "<ul>";
            foreach ($errors as $error) {
                echo "<li>$error</li>";
            }
            echo "</ul>";
        }
    }
    ?>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required><br>

        <label for="age">Age:</label>
        <input type="number" id="age" name="age" min="18" max="99" required><br>

        <label for="favorite_color">Favorite Color:</label>
        <select id="favorite_color" name="favorite_color" required>
            <option value="">Select a color</option>
            <option value="red">Red</option>
            <option value="blue">Blue</option>
            <option value="green">Green</option>
            <option value="yellow">Yellow</option>
            <option value="purple">Purple</option>
        </select><br>

        <input type="submit" value="Submit">
    </form>
</body>
</html>
```

This code creates the form and handles validation, error messages, and saving data to the database.

7. Start your XAMPP server and open a web browser. Navigate to http://localhost/user_form/ to view and test your PHP web application.