How can PHP be used to create a feature for adding and reading members in a website admin area?
To create a feature for adding and reading members in a website admin area using PHP, you can create a form for adding new members and a script to handle the form submission and display existing members. The form should collect necessary information such as username, email, and password. The PHP script should validate the form data, insert new members into a database, and retrieve existing members to display in the admin area.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "members";
$conn = new mysqli($servername, $username, $password, $dbname);
// Add new member
if(isset($_POST['submit'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO members (username, email, password) VALUES ('$username', '$email', '$password')";
$conn->query($sql);
}
// Retrieve existing members
$sql = "SELECT * FROM members";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Username: " . $row["username"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
// Close database connection
$conn->close();
?>