Insert, Delete, and Update record from the database using customize functions in PHP

0
596

In this tutorial you will learn how to insert, delete and update data from the database using customize function in PHP.

Before we proceed to the actual code, we need to create a database first.

After that we need to create an index.php file for the submission of the data using form tags

After you created index.php file you can now create the function.php file which will be use to insert,delete and update data from the database.

The codes below will show you on how to create a function that will connect us to the localhost or server.

After you created the function for connecting to the localhost or server you can now create an insert function that will be used in inserting data into the database.

As you can see in the above figure I used the $tblname and $form_data variables. The tblname is use to identify what table will the data inserted and the form_data variable is use to get the data that will be inserted into the database. Then after that I declare $fields variable as an array_keys($form_data)  to retrieve the field columns and then implode the array itself to build the centre part of the ‘insert’ query.

Now we can create a delete function that will delete the data from database

In the above figure I used tblname , field_id, and id variables same as what I did in the insert function but different is that the field_id and the id variables. The field_id will be used to identify what field name will be use in deleting the row in the database. The id variable is use to identify what specify row will be deleted.

Lastly, we can create the update function that will update the data from database.

As you can see in the figure above i used 4 variables this variables will be used in identifying what data will be updated.

 

Now we can use the functions that we created

We need to fetch the data from the database

As you can see in the figure above I have an edit.php and delete.php file which will be use to delete and update data.

After fetching the data from the database you can now create edit.php file to update the data.

After creating edit.php file we can now also create delete.php file to delete the record in the database.

 

 

In this way you can easily insert, delete, and update record from the database even if you have lots of tables in your database. Simple and easy.

Download “Insertdelete_update.zip” Insertdelete_update.zip – Downloaded 65 times – 2 KB