How can a PHP beginner create a login account system with user registration and centralized data retrieval?
To create a login account system with user registration and centralized data retrieval in PHP, you can use a combination of HTML forms for user input, PHP scripts to handle form submissions, and a MySQL database to store user information.
<?php
// Establish a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// User registration form handling
if(isset($_POST['register'])){
$username = $_POST['username'];
$password = $_POST['password'];
// Insert user data into the database
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
$conn->query($sql);
echo "User registered successfully!";
}
// User login form handling
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
// Retrieve user data from the database
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
if($result->num_rows > 0){
echo "Login successful!";
} else {
echo "Invalid username or password!";
}
}
// Centralized data retrieval
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "Username: " . $row['username'] . "<br>";
}
}
// Close the database connection
$conn->close();
?>