• We just launched and are currently in beta. Join us as we build and grow the community.

Insert and Delete Record in Database Using PHP/MySQL

charliehealy

Code Compiler
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
122
Likes
12
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. echo

    "<tr>"

    ;
  23. echo

    "<th><input type = 'text' name = 'stud_id' placeholder = 'Student ID'></th>"

    ;
  24. echo

    "<th><input type = 'text' name = 'last_name' placeholder = 'Last Name'></th>"

    ;
  25. echo

    "<th><input type = 'text' name = 'first_name' placeholder = 'First Name'></th>"

    ;
  26. echo

    "<th><input type = 'text' name = 'date_enrol' placeholder = 'Date Enrolled'></th>"

    ;
  27. echo

    "<th><input type = 'submit' name = 'submit' class='btn btn-primary' value='Add Student'></th>"

    ;
  28. echo

    "</tr>"

    ;
  29. $result

    =

    mysql_query

    (

    "SELECT * FROM students"

    )

    ;
  30. while

    (

    $row

    =

    mysql_fetch_array

    (

    $result

    )

    )

    {
  31. echo

    "<tr class='record'>"

    ;
  32. echo

    "<td>"

    .

    $row

    [

    'stud_id'

    ]

    .

    "</td>"

    ;
  33. echo

    "<td>"

    .

    $row

    [

    'last_name'

    ]

    .

    "</td>"

    ;
  34. echo

    "<td>"

    .

    $row

    [

    'first_name'

    ]

    .

    "</td>"

    ;
  35. echo

    "<td>"

    .

    $row

    [

    'date_enrol'

    ]

    .

    "</td>"

    ;
  36. echo

    '<td><a href="#" id="'

    .

    $row

    [

    'id'

    ]

    .

    '" class="delbutton" title="Click To Delete"><h1 class="btn btn-primary">Delete</h1></a></td>'

    ;
  37. echo

    "</tr>"

    ;
  38. }
  39. ?>
  40. </tr>
  41. </tbody>
  42. </table>
  43. </form>
  44. </div>
  45. <script src="js/jquery.js"></script>
  46. <script type="text/javascript">
  47. $(function() {
  48. $(".delbutton").click(function(){
  49. var element = $(this);
  50. var del_id = element.attr("id");
  51. var info = 'id=' + del_id;
  52. if(confirm("Are you sure do you want to delete this data? There is NO undo!"))
  53. {
  54. $.ajax({
  55. type: "GET",
  56. url: "delete.php",
  57. data: info,
  58. success: function(){
  59. }
  60. });
  61. $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
  62. .animate({ opacity: "hide" }, "slow");
  63. }
  64. return false;
  65. });
  66. });
  67. </script>

Delete.php - And for the delete function.

  1. <?php
  2. include

    (

    'dbcon.php'

    )

    ;

  3. if

    (

    $_GET

    [

    'id'

    ]

    )
  4. {
  5. $id

    =

    $_GET

    [

    'id'

    ]

    ;
  6. $sql_delete

    =

    "DELETE FROM students WHERE id ='$id

    '"

    ;
  7. mysql_query

    (

    $sql_delete

    )

    ;
  8. }
  9. ?>

Dbcon.php - For the database connection.

  1. <?php
  2. $mysql_hostname

    =

    "localhost"

    ;
  3. $mysql_user

    =

    "root"

    ;
  4. $mysql_password

    =

    ""

    ;
  5. $mysql_database

    =

    "data"

    ;

  6. $con

    =

    mysql_connect

    (

    $mysql_hostname

    ,

    $mysql_user

    ,

    $mysql_password

    )

    or die

    (

    "Please Check Your Database Connection."

    )

    ;
  7. mysql_select_db

    (

    $mysql_database

    ,

    $con

    )

    or die

    (

    "Please Check Your Database"

    )

    ;
  8. ?>

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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

330,760

330,768

Top