What is the best method for creating user profiles in PHP, especially in terms of storing additional user data like age and city?

When creating user profiles in PHP, the best method for storing additional user data like age and city is to use a database to store this information. You can create a table specifically for user profiles and include columns for the user's age and city. Then, when a user registers or updates their profile, you can insert or update the corresponding data in the database.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Create a table for user profiles
$sql = "CREATE TABLE user_profiles (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(30) NOT NULL,
    age INT(3),
    city VARCHAR(50)
)";

if ($conn->query($sql) === TRUE) {
    echo "Table user_profiles created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

// Insert user data into the user_profiles table
$username = "john_doe";
$age = 30;
$city = "New York";

$sql = "INSERT INTO user_profiles (username, age, city)
VALUES ('$username', '$age', '$city')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

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