What are the potential reasons for a PHP code not saving new user data to a database?

There could be several potential reasons for PHP code not saving new user data to a database. Some common issues could include incorrect database connection settings, errors in the SQL query syntax, lack of error handling, or insufficient permissions to write to the database. To solve this issue, ensure that the database connection settings are correct, double-check the SQL query for any errors, implement proper error handling to catch any issues, and ensure that the user has the necessary permissions to write to the database.

<?php
// Database connection settings
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Sample user data
$username = "john_doe";
$email = "john.doe@example.com";

// SQL query to insert user data
$sql = "INSERT INTO users (username, email) VALUES ('$username', '$email')";

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

// Close connection
$conn->close();
?>