Insert and Delete Record in Database Using PHP/MySQL
Submitted by rinvizle on Tuesday, December 6, 2016 - 17:15.
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.
Delete.php - And for the delete function.
Dbcon.php - For the database connection.
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.
Sample Code
Index.php - This is for the form of the webpage and for adding and deleting a data.- <div class="container">
- <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST">
- <table class="table table-hover">
- <thead>
- <tr style="background-color: cadetblue;">
- <th>STUDENT ID</th>
- <th>LAST NAME</th>
- <th>FIRST NAME</th>
- <th>DATE ENROLLED</th>
- <th>ACTIONS</th>
- </tr>
- <tr>
- <?php
- {
- $qry = mysql_query("INSERT INTO students (stud_id, last_name, first_name, date_enrol) VALUES ('$stud_id','$last_name','$first_name','$date_enrol')");
- }
- echo "<tr>";
- echo "<th><input type = 'text' name = 'stud_id' placeholder = 'Student ID'></th>";
- echo "<th><input type = 'text' name = 'last_name' placeholder = 'Last Name'></th>";
- echo "<th><input type = 'text' name = 'first_name' placeholder = 'First Name'></th>";
- echo "<th><input type = 'text' name = 'date_enrol' placeholder = 'Date Enrolled'></th>";
- echo "<th><input type = 'submit' name = 'submit' class='btn btn-primary' value='Add Student'></th>";
- echo "</tr>";
- echo "<tr class='record'>";
- echo "<td>". $row['stud_id'] ."</td>";
- echo "<td>". $row['last_name'] ."</td>";
- echo "<td>". $row['first_name'] ."</td>";
- echo "<td>". $row['date_enrol'] ."</td>";
- echo '<td><a href="#" id="'.$row['id'].'" class="delbutton" title="Click To Delete"><h1 class="btn btn-primary">Delete</h1></a></td>';
- echo "</tr>";
- }
- ?>
- </tr>
- </tbody>
- </table>
- </form>
- </div>
- <script src="js/jquery.js"></script>
- <script type="text/javascript">
- $(function() {
- $(".delbutton").click(function(){
- var element = $(this);
- var del_id = element.attr("id");
- var info = 'id=' + del_id;
- if(confirm("Are you sure do you want to delete this data? There is NO undo!"))
- {
- $.ajax({
- type: "GET",
- url: "delete.php",
- data: info,
- success: function(){
- }
- });
- $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
- .animate({ opacity: "hide" }, "slow");
- }
- return false;
- });
- });
- </script>
- <?php
- include('dbcon.php');
- if($_GET['id'])
- {
- $id = $_GET['id'];
- $sql_delete = "DELETE FROM students WHERE id ='$id'";
- }
- ?>
- <?php
- $mysql_hostname = "localhost";
- $mysql_user = "root";
- $mysql_password = "";
- $mysql_database = "data";
- $con = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die ("Please Check Your Database Connection.");
- ?>
Add new comment
- 285 views