What are the best practices for storing registration data in a MySQL database for a forum service?

When storing registration data in a MySQL database for a forum service, it is important to securely store sensitive information such as passwords by hashing them before insertion. Additionally, it is recommended to validate user input to prevent SQL injection attacks and ensure data integrity. Using prepared statements can help prevent these security vulnerabilities and ensure a robust registration system.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Validate user input
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);

// Insert user registration data into database using prepared statement
$stmt = $mysqli->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $email, $password);
$stmt->execute();

// Close statement and connection
$stmt->close();
$mysqli->close();