charliehealy
Code Compiler
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.
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.
Download
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
- if
(
isset
(
$_POST
[
'submit'
]
)
)
- {
- $stud_id
=
trim
(
strip_tags
(
addslashes
(
$_POST
[
'stud_id'
]
)
)
)
;
- $last_name
=
trim
(
strip_tags
(
addslashes
(
$_POST
[
'last_name'
]
)
)
)
;
- $first_name
=
trim
(
strip_tags
(
addslashes
(
$_POST
[
'first_name'
]
)
)
)
;
- $date_enrol
=
trim
(
strip_tags
(
addslashes
(
$_POST
[
'date_enrol'
]
)
)
)
;
- $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>"
;
- $result
=
mysql_query
(
"SELECT * FROM students"
)
;
- while
(
$row
=
mysql_fetch_array
(
$result
)
)
{
- 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>
Delete.php - And for the delete function.
- <?php
- include
(
'dbcon.php'
)
;
- if
(
$_GET
[
'id'
]
)
- {
- $id
=
$_GET
[
'id'
]
;
- $sql_delete
=
"DELETE FROM students WHERE id ='$id
'"
;
- mysql_query
(
$sql_delete
)
;
- }
- ?>
Dbcon.php - For the database connection.
- <?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."
)
;
- mysql_select_db
(
$mysql_database
,
$con
)
or die
(
"Please Check Your Database"
)
;
- ?>
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.