PHP - Assign User To Different Division In MySQLi

In this tutorial we will create a Assign User To Different Division In MySQLi using PHP. This code can divide the roles of individuals user when user submit the form inputs. The code use MySQLi SELECT query to display a specific data in the table row by providing a keyword in the WHERE clause division. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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_division, after that click Import then locate the database file inside the folder of the application then click ok. tut1

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.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_division");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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.
  1. <!DOCTYPE html>
  2. <?php require'conn.php'?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Assign User To Different Division In MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="save.php">
  20. <div class="form-group">
  21. <label>Name</label>
  22. <input type="text" class="form-control" name="name" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" min="0" class="form-control" name="username" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" class="form-control" max="12" name="password" required="required"/>
  31. </div>
  32. <div class="form-group">
  33. <label>Division</label>
  34. <select name="division" class="form-control" required="required">
  35. <option value="">Select an option</option>
  36. <option value="Marketing">Marketing</option>
  37. <option value="Finance">Finance</option>
  38. <option value="Operations management">Operations management</option>
  39. <option value="Human Resource">Human Resource</option>
  40. <option value="IT">IT</option>
  41. </select>
  42. </div>
  43. <center><button class="btn btn-primary" name="save">Save</button></center>
  44. </form>
  45. </div>
  46. <div class="col-md-8">
  47. <form method="POST" action="">
  48. <div class="form-inline">
  49. <label>Sort By Division: </label>
  50. <select name="division" class="form-control" required="required">
  51. <option value="">Select an option</option>
  52. <option value="Marketing">Marketing</option>
  53. <option value="Finance">Finance</option>
  54. <option value="Operations management">Operations management</option>
  55. <option value="Human Resource">Human Resource</option>
  56. <option value="IT">IT</option>
  57. </select>
  58. <button class="btn btn-success" name="sort">Sort</button>
  59. </div>
  60. </form>
  61. <br />
  62. <?php include'sort.php'?>
  63. </div>
  64. </div>
  65. </body>
  66. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the form inputs to the MySQLi server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $name=$_POST['name'];
  6. $username=$_POST['username'];
  7. $password=$_POST['password'];
  8. $division=$_POST['division'];
  9.  
  10. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$name', '$username', '$password', '$division')") or die(mysqli_error());
  11.  
  12. header('location: index.php');
  13. }
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will display a specific table row base on the given keyword 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 sort.php
  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_POST['sort'])){
  5. $division=$_POST['division'];
  6. ?>
  7. <table class="table table-bordered">
  8. <thead class="alert-info">
  9. <tr>
  10. <th>Name</th>
  11. <th>Username</th>
  12. <th>Password</th>
  13. <th>Division</th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <?php
  18. $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `division`='$division'") or die(mysqli_error());
  19. while($fetch=mysqli_fetch_array($query)){
  20. ?>
  21. <tr>
  22. <td><?php echo $fetch['name']?></td>
  23. <td><?php echo $fetch['username']?></td>
  24. <td>*********</td>
  25. <td><?php echo $fetch['division']?></td>
  26. </tr>
  27. <?php
  28. }
  29. ?>
  30. </tbody>
  31. </table>
  32. <?php
  33. }else{
  34. ?>
  35. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Name</th>
  39. <th>Username</th>
  40. <th>Password</th>
  41. <th>Division</th>
  42. </tr>
  43. </thead>
  44. <tbody>
  45. <?php
  46. $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  47. while($fetch=mysqli_fetch_array($query)){
  48. ?>
  49. <tr>
  50. <td><?php echo $fetch['name']?></td>
  51. <td><?php echo $fetch['username']?></td>
  52. <td>*********</td>
  53. <td><?php echo $fetch['division']?></td>
  54. </tr>
  55. <?php
  56. }
  57. ?>
  58. </tbody>
  59. </table>
  60. <?php
  61. }
  62. ?>
There you have it we successfully created Assign User To Different Division 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!

Comments

Add new comment