![](/uploads/1/2/5/3/125385832/689555227.jpg)
17 thoughts on “Insert, View, Edit and Delete Record from Database Using PHP and MySQL”. Pingback: Simple User Registration & Login Script in PHP and MySQL| HTML CSS PHP Tutorials. Reblogged this on Dinesh Ram Kali.
Last Updated - Aug 30, 2019
In this tutorial we will show you how to add, edit and delete records using jQuery, Ajax, PHP and MySQL.In this way you can do any modification in MySQL database dynamically means without refreshing your page.In this you can insert new rows in database, edit existing row and update the row in database and delete any row in database.You may also like delete multiple records from MySQL using PHP.
To Add, Edit And Delete Records It Takes Only Three Steps:-
- Make a PHP file to display database records
- Make a js file and define scripting
- Make a PHP file for database operations
![Php mysql ajax add edit delete printer Php mysql ajax add edit delete printer](/uploads/1/2/5/3/125385832/726311478.jpg)
Step 1.Make a PHP file to display database records
We make a PHP file and save it with a name display_records.php
In this step we create a database table 'user_detail' and insert some rows and then display the records to edit, delete and create a new row having textbox and buttons to insert new records in database.We insert modify_records.js file which we were going to create in next step.You may also like add, edit and delete rows from table dynamically using JavaScript.
Step 2.Make a js file and define scripting
We make a js file and save it with a name modify_records.js
In this step we create four funtions to edit, save, delete and insert records.In edit_row() function we get row id and get the name and age text and then insert the textbox with age and name value in both the columns to edit the text and then display the save button to save the records.
In save_row() function we get the row id and then with the help of row id get the name and age value then send the values to modify_records.php page to update the record and if the record updated successfully we display the edited record in there respective row.
In delete_row() function we again get the row id and send it to our modify_records.php page for deletion.
In insert_row() function we get the value of new_name and new_age from the textbox we made to insert new rows and send the data to modify_records.php page to insert new record in database and if record inserted successfully we display the new record in our table.
In save_row() function we get the row id and then with the help of row id get the name and age value then send the values to modify_records.php page to update the record and if the record updated successfully we display the edited record in there respective row.
In delete_row() function we again get the row id and send it to our modify_records.php page for deletion.
In insert_row() function we get the value of new_name and new_age from the textbox we made to insert new rows and send the data to modify_records.php page to insert new record in database and if record inserted successfully we display the new record in our table.
Step 3.Make a PHP file for database operations
We make a PHP file and save it with a name modify_records.php
In this step we create three isset() conditions to edit, delete and insert records in database.In first isset() condition we get all the three value and update the row having that particular row id and display 'success'.In second isset() condition we get the row id and delete the row having that id.In third isset() condition we get the name and age value and then insert in our database and then display the inserted id to create a new table row in our table in display_records.php page.Always validate data before and after submitting the form to prevent sql injections.
Thats all, this is how to add, edit and delete records using jQuery, Ajax, PHP and MySQL.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
RELATED TUTORIALS
- ❮
- ❯
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up Find file Copy path
Cannot retrieve contributors at this time
<?php |
$connect=mysqli_connect('localhost', 'root', '', 'ajax_live_add_delete'); |
$output=''; |
$sql='SELECT*FROM tbl_sample ORDER BY id DESC'; |
$result=mysqli_query($connect, $sql); |
$output.=' |
<div> |
<table> |
<tr> |
<th width='10%'>Id</th> |
<th width='40%'>First Name</th> |
<th width='40%'>Last Name</th> |
<th width='10%'>Delete</th> |
</tr>'; |
if(mysqli_num_rows($result) >0) |
{ |
while($row=mysqli_fetch_array($result)) |
{ |
$output.=' |
<tr> |
<td>'.$row['id'].'</td> |
<tdid'].'' contenteditable>'.$row['first_name'].'</td> |
<tdid'].'' contenteditable>'.$row['last_name'].'</td> |
<td><button type='button' name='delete_btn'id'].''>x</button></td> |
</tr> |
'; |
} |
$output.=' |
<tr> |
<td></td> |
<td contenteditable></td> |
<td contenteditable></td> |
<td><button type='button' name='btn_add'>+</button></td> |
</tr> |
'; |
} |
else |
{ |
$output.='<tr> |
<td colspan='4'>Data not Found</td> |
</tr>'; |
} |
$output.='</table> |
</div>'; |
echo$output; |
?> |
Copy lines Copy permalink
![](/uploads/1/2/5/3/125385832/689555227.jpg)