How To Generate Password To All Users In MySQL Using PHP | CampCodes ...

How to Generate Password To All Users In MySQL using PHP

July 14, 2020
how to generate password to all users in php

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

And, this is the link for the bootstrap that I used for the layout design

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.

how to generate password to all users with mysql using php

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.

    $conn=mysqli_connect("localhost", "root", "", "db_user_password");
        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">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <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">
                    <input type="text" class="form-control" name="firstname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="lastname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="username" required="required"/>
                <center><button class="btn btn-primary" name="insert" >Insert</button></center>
        <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 >
                        $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
                            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>";

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.

        mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$username', '')") or die(mysqli_error());

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.

    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());

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.