How can PHP be used to store user registration data in a database?
To store user registration data in a database using PHP, you can create a form where users input their information, then use PHP to process the form data and insert it into a database table. This can be achieved by connecting to the database, sanitizing and validating the user input, and executing an SQL query to insert the data into the database.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Process form data
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
// Sanitize and validate input (not shown here)
// Insert 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;
}
// Close the connection
$conn->close();
?>