PHP - Limit Maximum Post Submit

Submitted by: 
Language: 
Visitors have accessed this post 371 times.

In this tutorial we will create a Limit Maximum Post Submit using PHP. This code can limit the submitted post when user reach the maximum entries. The code use MySQLi POST method to call a specific function that prompt a warning when you reach the maximum entry for your database using mysqli_num_rows() to track the number of rows available in the table. 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_post_limit, 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_post_limit");
  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. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodster.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodster.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Limit Maximum Post Submit</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>Username</label>
  22. <input type="text" name="username" class="form-control" reqired="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Password</label>
  26. <input type="password" name="password" class="form-control" reqired="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Firstname</label>
  30. <input type="text" name="firstname" class="form-control" reqired="required"/>
  31. </div>
  32. <div class="form-group">
  33. <label>Lastname</label>
  34. <input type="text" name="lastname" class="form-control" reqired="required"/>
  35. </div>
  36.  
  37. <center><button class="btn btn-primary" name="save">Submit</button></center>
  38. </form>
  39. </div>
  40. <div class="col-md-8">
  41. <table class="table table-bordered">
  42. <thead class="alert-info">
  43. <tr>
  44. <th>Firstname</th>
  45. <th>Lastname</th>
  46. <th>Username</th>
  47. <th>Password</th>
  48. </tr>
  49. </thead>
  50. <tbody>
  51. <?php
  52. require'conn.php';
  53. $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  54. while($fetch=mysqli_fetch_array($query)){
  55. ?>
  56. <tr>
  57. <td><?php echo $fetch['username']?></td>
  58. <td><?php echo $fetch['password']?></td>
  59. <td><?php echo $fetch['firstname']?></td>
  60. <td><?php echo $fetch['lastname']?></td>
  61. </tr>
  62. <?php
  63. }
  64. ?>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. </body>
  70. </html>

Creating the Main Function

This code contains the main function of the application. This code will prompt a warning when the button is clicked. 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. $username=$_POST['username'];
  6. $password=$_POST['password'];
  7. $firstname=$_POST['firstname'];
  8. $lastname=$_POST['lastname'];
  9.  
  10.  
  11. $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  12. $row=mysqli_num_rows($query);
  13.  
  14. if($row < 5){
  15. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$username', '$password', '$firstname', '$lastname')") or die(mysqli_error());
  16. header('location:index.php');
  17. }else{
  18. echo"<script>alert('You have reached the maximum limit of 5 post \\nPlease delete some data first!')</script>";
  19. echo"<script>window.location='index.php'</script>";
  20. }
  21. }
  22. ?>

There you have it we successfully created Limit Maximum Post Submit 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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.