What are common methods for users to register on a PHP website and have their data automatically stored in a database?

When users register on a PHP website, their data needs to be securely stored in a database for future use. One common method to achieve this is by using SQL queries to insert the user's information into the database after they submit the registration form.

// 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);
}

// Get user input from registration form
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

// Insert user data into the database
$sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";

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

$conn->close();