Table of Contents
Tutorial: How to Generate Password To All Users In MySQL using PHP with Source Code
In this tutorial, we will create on How to Generate Password To All Users In MySQL using PHP. This code will generate a random password for all users when the user clicks the generate button. The system uses href or Hypertext Referencing to direct the user to another page that has a specific function for creating a password and adding it to the database using MySQLi Update query. This is a user-friendly kind of program feel free to modify it and use it as your own.
We will be using PHP as a scripting language that manages a database server to handle a bulk of data per transaction. It describes as an advanced technology that manages both server and control-block of your machine.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
Creating Database
Open your database web server, then create a database name in it db_user_password; after that, click Import, then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
<?php $conn=mysqli_connect("localhost", "root", "", "db_user_password"); if(!$conn){ die("Error: Failed to connect to database!"); } ?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Generate Password To All User In MySQLi</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-4"> <form method="POST" action="insert.php"> <div class="form-group"> <label>Firstname</label> <input type="text" class="form-control" name="firstname" required="required"/> </div> <div class="form-group"> <label>Lastname</label> <input type="text" class="form-control" name="lastname" required="required"/> </div> <div class="form-group"> <label>Username</label> <input type="text" class="form-control" name="username" required="required"/> </div> <center><button class="btn btn-primary" name="insert" >Insert</button></center> </form> </div> <div class="col-md-8"> <a href="generate.php" class="btn btn-success">Generate Password</a> <br /><br /> <table class="table table-bordered"> <thead class="alert-info"> <tr > <th>Firstname</th> <th>Lastname</th> <th>Address</th> <th>Password</th> </tr> </thead> <tbody> <?php require'conn.php'; $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ echo"<tr><td>".$fetch['firstname']."</td><td>".$fetch['lastname']."</td><td>".$fetch['username']."</td><td style='background-color:#f00; color:#fff;'>".$fetch['password']."</td></tr>"; } ?> </tbody> </table> </div> </div> </body> </html>
Creating the PHP Query
This code contains the php query of the application. This code will store the form inputs to the MySQLi database server. To make this just copy and write these block of codes below inside the text editor, then save it as insert.php.
<?php require_once'conn.php'; if(ISSET($_POST['insert'])){ $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $username=$_POST['username']; mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$username', '')") or die(mysqli_error()); header("location:index.php"); } ?>
Creating the Main Function
This code contains the main function of the application. This code will generate a random password when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as generate.php.
<?php require_once 'conn.php'; function randPass($len, $set = "") { $gen = ""; for($i = 0; $i < $len; $i++) { $set = str_shuffle($set); $gen.= $set[0]; } return $gen; } $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error()); while($fetch = mysqli_fetch_array($query)){ $id = $fetch['user_id']; $pass = randPass(12, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'); mysqli_query($conn, "UPDATE `user` SET `password` = '$pass' WHERE `user_id` = '$id'") or die(mysql_error()); } header("location:index.php"); ?>
There you have it we successfully created Generate Password To All User In MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!