How can PHP be used to create a registration form that generates a username and password upon submission and stores it in a MySQL database?
To create a registration form that generates a username and password upon submission and stores it in a MySQL database, you can use PHP to handle form submission, generate random usernames and passwords, and insert the data into the database using SQL queries.
<?php
// Connect to MySQL 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);
}
// Generate random username and password
$username = "user" . rand(1000, 9999);
$password = uniqid();
// Insert username and password into database
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Keywords
Related Questions
- In PHP, what are the potential security risks associated with not properly sanitizing user input before using it as variables in SQL queries?
- How can PHP be utilized to display different messages based on the number of teams registered in a form?
- How can the background color of cells be changed in FPDF using an array->row output?