Insert and Delete Record in Database Using PHP/MySQL

In this tutorial we will create a simple Insert and Delete Record in Database Using PHP/MySQL. This simple tutorial creates or insert a new data from our webpage to our database. You can also delete the data you inserted. The purpose of this simple tutorial is to show the live updates of every data you inserted and deleted. The application is programmed using Bootstrap Framework, PHP, and MySQL.

Sample Code

Index.php - This is for the form of the webpage and for adding and deleting a data.
  1. <div class="container">
  2. <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST">
  3. <table class="table table-hover">
  4. <thead>
  5. <tr style="background-color: cadetblue;">
  6. <th>STUDENT ID</th>
  7. <th>LAST NAME</th>
  8. <th>FIRST NAME</th>
  9. <th>DATE ENROLLED</th>
  10. <th>ACTIONS</th>
  11. </tr>
  12. <tr>
  13. <?php
  14. if(isset($_POST['submit']))
  15. {
  16. $stud_id = trim(strip_tags(addslashes($_POST['stud_id'])));
  17. $last_name = trim(strip_tags(addslashes($_POST['last_name'])));
  18. $first_name = trim(strip_tags(addslashes($_POST['first_name'])));
  19. $date_enrol = trim(strip_tags(addslashes($_POST['date_enrol'])));
  20. $qry = mysql_query("INSERT INTO students (stud_id, last_name, first_name, date_enrol) VALUES ('$stud_id','$last_name','$first_name','$date_enrol')");
  21. }
  22.  
  23. echo "<tr>";
  24. echo "<th><input type = 'text' name = 'stud_id' placeholder = 'Student ID'></th>";
  25. echo "<th><input type = 'text' name = 'last_name' placeholder = 'Last Name'></th>";
  26. echo "<th><input type = 'text' name = 'first_name' placeholder = 'First Name'></th>";
  27. echo "<th><input type = 'text' name = 'date_enrol' placeholder = 'Date Enrolled'></th>";
  28. echo "<th><input type = 'submit' name = 'submit' class='btn btn-primary' value='Add Student'></th>";
  29. echo "</tr>";
  30. $result = mysql_query("SELECT * FROM students");
  31. while($row = mysql_fetch_array($result)){
  32. echo "<tr class='record'>";
  33. echo "<td>". $row['stud_id'] ."</td>";
  34. echo "<td>". $row['last_name'] ."</td>";
  35. echo "<td>". $row['first_name'] ."</td>";
  36. echo "<td>". $row['date_enrol'] ."</td>";
  37. echo '<td><a href="#" id="'.$row['id'].'" class="delbutton" title="Click To Delete"><h1 class="btn btn-primary">Delete</h1></a></td>';
  38. echo "</tr>";
  39. }
  40. ?>
  41. </tr>
  42. </tbody>
  43. </table>
  44. </form>
  45. </div>
  46. <script src="js/jquery.js"></script>
  47. <script type="text/javascript">
  48. $(function() {
  49. $(".delbutton").click(function(){
  50. var element = $(this);
  51. var del_id = element.attr("id");
  52. var info = 'id=' + del_id;
  53. if(confirm("Are you sure do you want to delete this data? There is NO undo!"))
  54. {
  55. $.ajax({
  56. type: "GET",
  57. url: "delete.php",
  58. data: info,
  59. success: function(){
  60. }
  61. });
  62. $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
  63. .animate({ opacity: "hide" }, "slow");
  64. }
  65. return false;
  66. });
  67. });
  68. </script>
Delete.php - And for the delete function.
  1. <?php
  2. include('dbcon.php');
  3.  
  4. if($_GET['id'])
  5. {
  6. $id = $_GET['id'];
  7. $sql_delete = "DELETE FROM students WHERE id ='$id'";
  8. mysql_query($sql_delete);
  9. }
  10. ?>
Dbcon.php - For the database connection.
  1. <?php
  2. $mysql_hostname = "localhost";
  3. $mysql_user = "root";
  4. $mysql_password = "";
  5. $mysql_database = "data";
  6.  
  7. $con = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die ("Please Check Your Database Connection.");
  8. mysql_select_db($mysql_database, $con) or die("Please Check Your Database");
  9. ?>
Hope that you learn in this tutorial. And for more updates and programming tutorials don't hesitate to ask and we will answer your questions and suggestions. Don't forget to LIKE & SHARE this website.

Add new comment