Bootstrap and php and add a new admin user
Here's a step-by-step guide on how to add a new admin user using Bootstrap and PHP:
Prerequisites:
- You have a PHP script that connects to your database and has a function to add a new user.
- You have a Bootstrap-based HTML file that displays a form to add a new user.
- You have a database table named
users
with columnsid
,username
,password
,email
, androle
(withadmin
as the default role).
Step 1: Create the HTML form using Bootstrap
Create a new HTML file (e.g., add-admin-user.html
) and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>Add Admin User</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-center">Add Admin User</h1>
<form action="add-admin-user.php" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<button type="submit" class="btn btn-primary">Add User</button>
</form>
</div>
</body>
</html>
This form has three input fields: username
, password
, and email
. The form action is set to add-admin-user.php
, which is the PHP script that will handle the form submission.
Step 2: Create the PHP script to add a new admin user
Create a new PHP file (e.g., add-admin-user.php
) and add the following code:
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get the form data
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
// Hash the password
$password_hash = password_hash($password, PASSWORD_DEFAULT);
// Add the new user to the database
$query = "INSERT INTO users (username, password, email, role) VALUES ('$username', '$password_hash', '$email', 'admin')";
$result = mysqli_query($conn, $query);
// Check if the query was successful
if (!$result) {
die("Error adding user: ". mysqli_error($conn));
}
// Close the database connection
mysqli_close($conn);
// Redirect to a success page
header("Location: success.php");
exit;
?>
This PHP script connects to the database, gets the form data, hashes the password using the password_hash()
function, and adds the new user to the users
table with the admin
role. If the query is successful, it redirects the user to a success page.
Step 3: Create the success page
Create a new HTML file (e.g., success.php
) and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>Success</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-center">User added successfully!</h1>
<p>You can now log in with your new admin credentials.</p>
</div>
</body>
</html>
This success page displays a message indicating that the user was added successfully.
Putting it all together
Save all the files in the same directory and open add-admin-user.html
in a web browser. Fill out the form and submit it. The PHP script will add the new user to the database and redirect you to the success page.
Note: Make sure to replace the database connection details (e.g., localhost
, username
, password
, database_name
) with your actual database credentials.